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

Solution 1:

I considered editing @ChrisMoschini's post, but thought it was different enough. My issue was that I started a new MVC5 application, and blindly copied over too many web.config settings from an old MVC3 project I wanted to use as a template/starting point. Doing this caused me to have some invalid versions referenced in my web.config.

To fix, I created another new MVC5 project and made sure the following config values in my bad project matched the vanilla, unmodified MVC5 app. Again, do not blindly copy these version numbers. Just make sure they match a vanilla MVC app of the version that you're trying to get to work

in root web.config:

<appSettings>
    ...
    <add key="webpages:Version" value="3.0.0.0"/> 
    ...
</appSettings>
<system.web>
    ...
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.1"/>
    ...
</system.web>

in the Views\Web.config:

<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>


<system.web.webPages.razor>
  ...
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  ...
</system.web.webPages.razor>

Solution 2:

A few of my projects had this issues for months. I tried so many workarounds and nothing worked. The issues seemed to be with the projects: the same project will have Intellisense issue across all PCs with Visual Studio. Finally I came through this post: http://www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/

In our case the cause was the output directory of all those projects were changed to other than "bin\". By changing the OutputPath back to "bin\" (and cleaning the project, closing then re-opening Visual Studio), I got Intellisense back.

The post referenced above also provided a workaround to maintain a separate output directory while still having Intellisense.

Hope this helps someone someday.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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.