Visual Studio 2015 not syntax highlighting razor n

2019-01-05 08:10发布

My Razor views in VS2015 RC are not showing the proper coloring for C# code. My project was working fine in VS2013, but it isn't in 2015, and it's not giving me any Intellisense on the C# code. The solution builds and the site runs fine.

I tried the following to no avail

  • disabling all Visual Studio extensions
  • deleting my .suo file
  • removing the project and re-adding it to the solution

How can I fix this?

Example:

21条回答
做个烂人
2楼-- · 2019-01-05 08:38

In my Web.config I had this:

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <add namespace="LinkApp.Models.Templates"/>
            <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="LinkApp" />
          </namespaces>
        </pages>
      </system.web.webPages.razor>

Oddly enough, I moved my custom namespace to the bottom, and that fixed it, like this:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.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="LinkApp" />
        <add namespace="LinkApp.Models.Templates"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-05 08:39

I had the same problem, the only fix so far is by resetting all user data (C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe /ResetUserData).

NOTE: You will lose all customizations, e.g. keyboard shortcuts and window layouts if you do this!

EDIT: People are saying this only works for RC, not RTM, so you might want to try the other fixes first.

查看更多
做个烂人
4楼-- · 2019-01-05 08:39

Friendly reminder to make sure you have correctly used the lowercase @model at the top of your view, and not @Model (like I made the mistake of doing).

Lowercase @model is a directive that strongly types the view to an instance of the class that follows it. Whereas the capitalized Model is a property of the view that gets the model instance.

查看更多
叼着烟拽天下
5楼-- · 2019-01-05 08:40

I fixed the issue by including the file in the project/solution. I'd overlooked the fact that there's no highlighting/intellisense if it's not included.

查看更多
【Aperson】
6楼-- · 2019-01-05 08:43

Tools > Options > Text Editor > All Languages > General. Uncheck the Hide advanced members option. Click OK.

Once I did that Intellisense seemed to work for me. I then reset it back and kept functionality. I'm using Visual Studio 2015 enterprise edition.

I had tried ResetUserData and that did not work for me.

查看更多
聊天终结者
7楼-- · 2019-01-05 08:44

I've found in this answer another way to fix it without the devenv.exe /ResetUserData

Just delete the contents of this directory with Visual Studio closed: %LocalAppData%\Microsoft\VisualStudio\<visual_studio_version_number>\ComponentModelCache

To find the version number of the Visual Studio edition your are running please refer this list. Note that only the major number is important, the minor version can and will probably differ. In my case I had Visual Studio 2017 Enterprise installed so I looked for 15.xx and found 15.0_9a1c4a06 inside the AppData folder.

查看更多
登录 后发表回答