MVC Razor view Intellisense broken in VS 2013/2015/2017

后端 未结 22 2227
失恋的感觉
失恋的感觉 2020-11-27 16:52

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

相关标签:
22条回答
  • 2020-11-27 17:42

    I have recently solved this problem myself. I upgraded from MVC4 to MVC5 (specifically 5.1). Upgrading to a newer version of MVC caused this havoc and I spent hours trying to solve it. Minor changes to the Web.Config file fixed the intellisense issue!

    You said the project works in VS2010, but not 2013? See this answer here.

    I recommend upgrading to MVC5. It's not painful and the upgrade should be pretty seamless.

    If you upgrade to MVC5 and you're still not getting intellisense, you need to update the Web.Config file manually as the upgrade may not do this correctly!

    Here's a modified version of your Web.conifg in the /Views folder that should reflect changes for MVC5.

    <?xml version="1.0"?>  
    <configuration>
    <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.1.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=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <controls>
                <add assembly="System.Web.Mvc, Version=5.1.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>
    

    If upgrading is not an option, then I'm afraid you will need to continue using VS2010 if you want intellisense support for MVC3. However, I strongly suggest upgrading to MVC5.

    0 讨论(0)
  • 2020-11-27 17:42

    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!

    0 讨论(0)
  • 2020-11-27 17:44

    MS says that for VS2013 "Intellisense for Razor (CSHTML and VBHTML) files is limited to HTML markup."

    But if you add these two lines inside each .cshtml the intellisense will work again for MVC3 in VS2013:

    @using System.Web.Mvc.Html
    @inherits System.Web.Mvc.WebViewPage<dynamic>
    

    Instead of dynamic you can put your Model's type.

    0 讨论(0)
  • 2020-11-27 17: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!

    0 讨论(0)
提交回复
热议问题