87

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>
silkfire
  • 24,585
  • 15
  • 82
  • 105
ridermansb
  • 10,779
  • 24
  • 115
  • 226
  • 1
    Try updating your nuget packages and also install VS2013 update 1. – Alex Dresko Jan 23 '14 at 14:05
  • Try creating a brand new ASP MVC project and determine if intellisense works then. – Alex Dresko Jan 23 '14 at 15:55
  • Works on new projects. this project is that it does not work! – ridermansb Jan 23 '14 at 18:01
  • 1
    Divide and conquer, my friend. There's some important difference between your broken project and a new project. You need to figure out what that difference is. – Alex Dresko Jan 23 '14 at 21:15
  • * I copied the web.config in the views folder to my project (did not work) * I checked the packages installed, dlls (and versions) and are all ok. * I tried to reinstall the packages using `Update-Package -Reinstall` (did not work) – ridermansb Jan 24 '14 at 10:35
  • Do you know of anything else I can verify that on project is working and compare with mine? – ridermansb Jan 24 '14 at 10:36
  • possible duplicate of [MVC Razor view Intellisense broken in VS 2013](http://stackoverflow.com/questions/22832435/mvc-razor-view-intellisense-broken-in-vs-2013) – Chris Moschini Jul 25 '14 at 00:43
  • Marking this as a dupe of a newer question because the newer question provides more analysis of the problem. – Chris Moschini Jul 25 '14 at 00:44

21 Answers21

145

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.

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • 4
    This worked great for me on a fully up to date VS2015U1 just now (just deleting the cache) – Ruben Bartelink Feb 15 '16 at 16:06
  • 4
    I just deleted the cache and it was fixed. Thanks. – ADM-IT Apr 27 '16 at 13:16
  • 1
    After creating a new area this issue started occurring to me for its new views. I just deleted the cache files and reopened VS and now it's working. Thanks! – Diego Cotini Oct 18 '16 at 17:25
  • VS2017: IntelliSense sometimes breaks like this after a VS exception (no useful reason given) is produced when opening a .cshtml file. In addition to the above actions, removing the MVC app project from my main solution and re-adding it seems to fix it. – Paul Lambert Mar 30 '17 at 03:31
  • 1
    Wow, super, thank you! The ComponentModelCache was my to-go place in VS 2013/15, I always had a shortcut to the folder handy: if anything breaks, delete the folder. VS 2017 is less prone to cache corruption, so I almost forgot about it. My problem was exactly same: no IS in Razor, and deleting the folder fixed it. Hope they'll get the cache coherent by VS 2042... – kkm inactive - support strike Dec 29 '18 at 20:51
  • I got this problem when I added an MVC area, this solution doesn't work for me, I also tried unloading and reloading the solution from my main project, and restarted VS, any help? – Mayer Spitz Feb 26 '19 at 17:45
  • 1
    Lost 2 hrs on this, it finally worked for me when after executing devenv /setup. – Marcelo De Zen Apr 13 '20 at 10:29
  • I had to google this up, so I would add that running Developer Command Prompt for VS (as an Administrator) - I have VS 2019 Pro: **1.** run cmd as admin **2.** `CD` to the project root folder and **3.** run `"c:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat"` – myro May 28 '20 at 07:27
  • Working Fine. Super :) – Thulasiram Nov 06 '20 at 14:08
  • %LOCALAPPDATA%\Local\Microsoft\VisualStudio\16.0_7b5fb5e8\ComponentModelCache - VS 2019 – Thulasiram Nov 06 '20 at 14:26
  • Only thing that worked for me was starting as admin Visual Studio 2019 Preview. While trying out .Net 6.0 (6.0.0-preview.3.21201.13). MS will probably fix it on final release. – Igor Jul 03 '21 at 21:51
  • Why make it so difficult? I just made a copy of the razor pages to somewhere (make some minor change, save) and delete the original from inside the project and add them back as existing items to the project. After that, the intellisense went back to normal and seems connected ok. – Jenna Leaf Mar 01 '23 at 01:38
25

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.

Community
  • 1
  • 1
Mike
  • 3,641
  • 3
  • 29
  • 39
  • 12
    Sometimes step 1 is enough – ksaylor11 Sep 14 '15 at 16:38
  • 3
    I had to show hidden files in order to find my solution options (.suo) file – SteveB Sep 03 '19 at 21:08
  • 2
    My .suo file was under ```/{solutionfolder}/.vs/{solutionname}/v16/``` and was named ```.suo```. – ctc Feb 28 '21 at 19:49
  • Sounds like .suo was corrupted or something. – Jenna Leaf Mar 01 '23 at 01:54
  • I just copy the bad pages out to somewhere outside the project, make some unimportant change, save, then delete the original from inside the project. Re-open the project, add those pages back as existing items. After that, intellisense comes right back as usual. – Jenna Leaf Mar 01 '23 at 02:00
15

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" />
John
  • 3,512
  • 2
  • 36
  • 53
  • 1
    Indeed rolling back to version 2 brought intelligence back to life. But my System.Web,Webpages reference is actually version 3.0.0 and my project is MVC 5! That's weird – Anestis Kivranoglou Jul 30 '15 at 18:10
  • I think mvc 5 ships with webpages version 3. That sounds correct. Perhaps your referenced dll's are not correct ? – John Jul 31 '15 at 12:23
  • Checked the dlls also, versions match. Even creating a new MVC 5 project from scratch has broken intellisence. I have VS 2012 Update 4 installed + Web Tools 2013.1 – Anestis Kivranoglou Jul 31 '15 at 13:20
  • 1
    Just found the issue..... it was Xamarin plug in which was causing the bug, and it's a known issue. – Anestis Kivranoglou Jul 31 '15 at 16:59
14

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>
Kevin Hirst
  • 876
  • 8
  • 17
  • My views/webconfig file looks exactly like this, and still.. same problem :( – Scuba Steve Oct 21 '16 at 23:11
  • 2
    This is what worked for me in combination with deleting the ComponentModelCache. – WillC Feb 28 '17 at 17:42
  • Just updated the versions based on this and also deleted the ComponentModelCache – bets Jun 15 '17 at 06:41
  • Finally! That worked for me too. Also in combination with deleting the ComponentModelCache after i updated the web.config in the views folder – Mevius Dec 12 '18 at 18:22
6

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.

Philipo55
  • 101
  • 1
  • 3
4

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

Palanikumar
  • 6,940
  • 4
  • 40
  • 51
4

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" />
  • Mine for some reason was set to 5 (in VS2017) changing to 3 appears to have solved the issue – bsod_ Oct 15 '18 at 10:44
4

In my case the solution was setting the "Use legacy Razor editor for ASP.NET Core" inside tools>options>text editor>HTML>advanced to true.

Dharman
  • 30,962
  • 25
  • 85
  • 135
3

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

Nigel G
  • 41
  • 5
  • 1
    for VS 2012 I tried the 'VS 2012 with ASP.Net and Web tools 2013.1' and it works: http://www.microsoft.com/en-au/download/details.aspx?id=41532 – alital Feb 24 '15 at 00:48
1

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.

mkimmet
  • 749
  • 3
  • 15
  • 27
  • 1
    I had this issue in VS Proffessional 2019 while working on an Umbraco project, and this solved the issue for me as well. Thanks for the tip! – GittinJiggyWithIt Dec 01 '20 at 08:37
1

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

Sheldon
  • 51
  • 3
1

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.

Eng. Samer T
  • 6,465
  • 6
  • 36
  • 43
1

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.

Jack
  • 170
  • 2
  • 15
0

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.

Thorsten Westheider
  • 10,572
  • 14
  • 55
  • 97
  • just curious - how was the Web.config in the Areas/Views/ the culprit? Was the fix basically along the lines of Kevin Hirst answer lower? – Don Cheadle Jul 12 '17 at 14:00
0

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.

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
csharpsql
  • 2,190
  • 1
  • 19
  • 24
0

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!

Roland Wales
  • 73
  • 1
  • 7
0

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.

NewUser
  • 1
  • 1
0

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

0

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

Volodymyr Kotylo
  • 483
  • 5
  • 11
0

I solved this problem by deleting the folder .vs in the solution path

0

None of the solutions above solved my case. Now I figured out it was because I did defragmentation. It caused this problem by moving and deleting some files. I re-installed Visual Studio. It works fine unless I perform defragmentation again. I hope this helps you