MVC Razor view Intellisense broken in VS 2013/2015

2020-01-24 21:08发布

I have an existing project written in VS2010 which when loaded in VS2010 works perfectly.

When I load this same project in VS2013 the MVC Razor views contain lots of errors as if the config file is missing from the views folder.

It appears to have not loaded the Razor editor correctly using the config files from both the root and the views folder and instead gives me errors like ...

The name 'model' does not exist in the current context

and ...

'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'LabelFor' 
and no extension method 'LabelFor' accepting a first argument of type 
'System.Web.WebPages.Html.HtmlHelper' could be found 
(are you missing a using directive or an assembly reference?)

...

Any idea what would cause this?

Edit: Config files as requested ....

From main web.config file (not all of it as it's way too big to post)

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Ninject" culture="neutral" publicKeyToken="c7192dc5380945e7" />
            <bindingRedirect newVersion="3.0.0.0" oldVersion="0.0.0.0-3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Validation" culture="neutral" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect newVersion="5.0.505.0" oldVersion="0.0.0.0-5.0.505.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Config file in "~/Views/" ...

<?xml version="1.0"?>  
<configuration>
    <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
            <namespaces>
                <add namespace="System.Web.Mvc" />
                <add namespace="System.Web.Mvc.Ajax" />
                <add namespace="System.Web.Mvc.Html" />
                <add namespace="System.Web.Routing" />
                <add namespace="Emedia.Common.Mvc.Views.Helpers"/>
                <add namespace="Emedia.Common.Mvc.Views.Extensions"/>
        <add namespace="Emedia.Common.Utilities"/>
        <add namespace="Emedia.Common.Utilities.Extensions"/> 
        <add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>
                <add namespace="Emedia.Resources.Service"/>
        <add namespace="Emedia.Subscriber.Controllers"/>
        <add namespace="Emedia.Subscriber.Controllers.ViewModels"/>
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>

    <system.web>
        <httpHandlers>
            <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>
        <pages
            validateRequest="false"
            pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <controls>
                <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
        </controls>
    </pages>
</system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
        <remove name="BlockViewHandler"/>
        <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>

22条回答
放荡不羁爱自由
2楼-- · 2020-01-24 21:44

I've changed from

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
 Version=5.2.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />

to this:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
 Version=5.0.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />

And it worked!

查看更多
地球回转人心会变
3楼-- · 2020-01-24 21:45

In upgrading from MVC 3 to 5, I found that in my root directory Web.config that the appSettings key webpages:version was set to 2.0.0.0. Changing this to 3.0.0.0 fixed this issue.

<appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> </appSettings>

查看更多
Explosion°爆炸
4楼-- · 2020-01-24 21:45

I upgraded an MVC3 project to MVC5, and did everything I could to avoid the only true answer to this problem that Microsoft recommends:

Start over with a new Project, and port the resulting bits over into your existing project.

That process is a big waste of time, and it seems like Microsoft should make a real upgrade path viable, but I am here to tell you the problem here is an emotional one not technical: You really do need to just create a new MVC5 project, and replace the following with the result of that new MVC5 project:

\Packages\*
\Project\Project.csproj
\Project\packages.config
\Project\Web.Config
\Project\Views\Web.Config
\Project\Areas\*\Views\Web.Config

If you don't you'll just go around in circles for eternity trying to find the one setting that's blowing things up. In our case, I had the Web.Configs all identical to the newly-created, Razor Intellisense-working MVC5 project, I had run every variety of upgrade tool I could find, you name it. Intellisense refused to work.

After just blowing away the .csproj and web.configs etc, Intellisense came magically back to life. Diffing the 2 sides, none of the answers that I've found anywhere match with what I'm seeing. Web.Configs are almost identical, and the bits that are different should be irrelevant. The main change is really what's in \Packages\ - a lot of older Razor, MVC, and WebPages dlls gone. That could be the trick, but skip that and save yourself a lot of time: Just make a new MVC5 project and dump the above-listed files over. It's the only sane method of upgrading.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-01-24 21:45

I eventually had a bunch of other problems on my pc because of a network server crash and ended up reinstalling visual studio.

This apparently solved the problem ... i have no idea how but it did.

I wonder if maybe in my case it was simply just a faulty installation rather than the typical problem.

For that reason I will mark this as the answer but +1 all other replies as they are potentially good answers to this problem.

I did however find that Microsoft clearly states visual studio 2013 does not support intellisense on lower versions of MVC than 4 so if you are using MVC 3 upgrade your project if you are using a newer version of MVC and nothing else here works try reinstalling visual studio.

Also worth noting ... MVC is now a nuget package so don't install MVC from the download redist allow VS to figure that out for you.

查看更多
对你真心纯属浪费
6楼-- · 2020-01-24 21:51

In my case I moved all of the views from an Area to the root Views folder, so I think VS got confused as to where my web.config was. I renamed it to Web.config, from web.config and then made an edit to the contents of the config (such as changing the version number of the razor host factory dll from 5.2.2.0 to 5.2.3), but then changed it back.

Then I went on a walk for about 30 minutes and came back and restarted VS and it was fixed!

查看更多
走好不送
7楼-- · 2020-01-24 21:53

I had this issue for six months and just realized that I needed to move my @model directives to the top of the pages. I previously had them inside code blocks.

查看更多
登录 后发表回答