IntelliSense in Razor files (.cshtml) stopped working

Intellisense does not work in razor files:

enter image description here

In my web.conifg file (in the Views folder) is apparently correct:

<?xml version="1.0" encoding="utf-8"?>
<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.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="System.Web.Optimization" />

                <add namespace="MvcSiteMapProvider.Web.Html" />
                <add namespace="MvcSiteMapProvider.Web.Html.Models" />

                <add namespace="DevTrends.MvcDonutCaching" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

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

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

Solution 1:

This is what worked for me after IntelliSense suddenly began to bug out and stopped colouring C# code correctly in between the HTML tags in my views:


Just delete the contents of the folder at %LOCALAPPDATA%\Microsoft\VisualStudio\16.0_<hash>\ComponentModelCache


enter image description here

As an additional step, you can optionally run the command DevEnv.exe /setup in Developer Command Prompt for VS (as an Administrator) if the above step doesn't resolve the issue.

Solution 2:

When intellisense stops working in the razor file, there's a good chance that the issue can be fixed in three steps:

  1. Close Visual Studio
  2. Delete the solution user options file (<solution-name>.suo)
  3. Re-open the solution in Visual Studio.

Other solutions for intellisense options can be found here.

Solution 3:

One cause for this could be if your wepages are set to version 3 for an mvc4 application, you can just change it to version 2 in the web.config app settings.

  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />

Solution 4:

I was experiencing this in Visual Studio 2013 after upgrading the project to MVC 5.2.3. The thing that worked for me was replacing the web.config in the "Views" folder with the following, since the NuGet package left that web.config with many MVC 4.0 references.

<?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.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.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="InvestureApps" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

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

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

Solution 5:

I had this issue with Visual Studio 2017 - I went into the directory where the solution is located and deleted .the vs folder (it's a hidden folder) and that fixed my issue.