IntelliSense in Razor files (.cshtml) stopped work

2020-01-26 04:12发布

问题:

Intellisense does not work in razor files:

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>

回答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\14.0\ComponentModelCache

If you're using VS 2019, the version folder is named 16.0_<hash>.


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.



回答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.



回答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" />


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


回答5:

In My case "Xamarin" (Mobile app development tools) is the problem. After uninstalled Xamarin, the razor Intellisense working fine.

http://forums.xamarin.com/discussion/24892...



回答6:

I had the same issue. Although I'm using VS 2012, there is a download that applies to both VS 2013 and VS 2012. I've tried quite a few fixes (having to do with web.config, uninstalling/reinstalling MVC via NuGet, etc) but the only one that worked for me was installing this (again, for me, for VS 2012)

VS 2013 with ASP.NET and Web Tools 2013.1 http://www.asp.net/downloads



回答7:

My project was upgraded from VS2012 to VS2013 and this solved the problem for me:

Change "webpages:Version" in the Web.config file from "1.0.0.0" to "2.0.0.0"

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


回答8:

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.



回答9:

It looks that there are many reasons produce this problem.

In my case I copied some code/files from another project, after that the Intellisense doesn't work, this because I have different namespace in copied files so that makes Intellisense doesn't work. correct the namespace makes it works again.



回答10:

Watch out if you have areas in your web application, there's yet another Web.config file in the Areas/Views folder and in my case this configuration file was the culprit.

Also, you may have to restart Visual Studio after making the changes to Web.config, rebuilding the solution doesn't seem to pick up the changes, at least it didn't for me.



回答11:

I just got through a very similar problem: an existing application wasn't providing intellesense for Razor views, but I could create a brand new application and it worked. I was able to solve the problem by using Scaffolding to create a new controller and views for an entity. The intellesense worked for the new entity, and it worked again for existing ones!



回答12:

Installing ASP.NET and Web Tools 2013.1 for Visual Studio 2012 worked for me:

You might need to restart VS for this to work.



回答13:

After lots of search and test I found that, without installing 2013 or any upgraded version you can still resolve this coloring issue for 2012. Installing "ASP.NET and Web Tools 2013.1 for Visual Studio 2012" solved this for me. Close VS 2012 and restart it after installing.

Dowload link is, https://www.microsoft.com/en-us/download/details.aspx?id=41532

It worked for me. Hope it works for you all.



回答14:

I had a similar situation, and then realized that the razor view wasn't included in my project. As soon as I included it in the project (right click the view file and select Include in Project), Intellisense showed up.



回答15:

In my case this fixed my problem i just change version value from 3 to 2. and its works for me.



回答16:

I just had to right click "Add to project" for my new view to pick up on Intellisense.



回答17:

Installing newer version (nightly build) of Resharper (2018.3 EAP 3) helped in my case.



回答18:

In my case, none of the aboves worked, here is what I did to resolve my problem:

Load the project with VS and leave it, and then open the your-project-name.csproj with Notepad++ or whatever the Text editor you like and made some changes, like delete a Content tag(Don't worry, it will be back once you reinclude it in VS Solution Explorer), once you save your changes, the Visual Studio IDE will detect your changes and ask you for reloading the whole project, click on 'Yes' and my code IntelliSense back.

I'm using: Microsoft Visual Studio Professional 2019 Preview Version 16.3.0 Preview 2.0

Hope it helps.