C# Razor 语法突出显示在 VS 2012 与 MVC 5 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19051550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:59:13  来源:igfitidea点击:

Razor syntax highlighting not working in VS 2012 with MVC 5

c#razorvisual-studio-2012visual-studio-2013asp.net-mvc-5

提问by Andreas

I'm playing around with MVC 5 RC 1 in Visual Studio 2013 RC. Works very well.

我在 Visual Studio 2013 RC 中玩弄 MVC 5 RC 1。效果很好。

Now I upgraded an existing MVC 4 project in VS 2012 to MVC 5 the same way as described here

现在,我按照此处所述的相同方式将 VS 2012 中的现有 MVC 4 项目升级到 MVC 5

I also changed the web.config(s) - see "upgrading from MVC4 to MVC5"

我还更改了 web.config(s) - 请参阅“从 MVC4 升级到 MVC5

Everything (build, run web app, even intellisense) works perfectly except the syntax highlighting of Razor/C# code in views.

除了视图中 Razor/C# 代码的语法高亮显示之外,一切(构建、运行 Web 应用程序,甚至智能感知)都可以完美运行。

I also tried it with an MVC 5 project created in VS 2013. Same result. So I assume VS 2012 doesn't "understand" the new assemblies?!

我还使用在 VS 2013 中创建的 MVC 5 项目进行了尝试。结果相同。所以我认为 VS 2012 不“理解”新程序集?!

Any known workaround or ideas to get highlighting back?

任何已知的解决方法或想法来突出显示?

采纳答案by Andreas

Microsoft released a new version of ASP.NET Web Tools for VS 2012. After installing syntax highlighting is back again.

Microsoft 为 VS 2012 发布了新版本的 ASP.NET Web Tools。安装语法高亮后又回来了。

http://blogs.msdn.com/b/webdev/archive/2013/11/18/announcing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012.aspx

http://blogs.msdn.com/b/webdev/archive/2013/11/18/annoucing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012。 aspx

Installing VS 2012 Update 4 was not enough.

安装 VS 2012 Update 4 是不够的。

A set of offline installers is available. The offline installers are easier to use than the Web Platform Tools invoker (WebNode11Pack.exe @ ~114KB):

有一组离线安装程序可用。离线安装程序比 Web 平台工具调用程序 (WebNode11Pack.exe @ ~114KB) 更易于使用:

If you want an offline installer then please goto http://www.microsoft.com/en-us/download/details.aspx?id=41532

如果您想要离线安装程序,请访问 http://www.microsoft.com/en-us/download/details.aspx?id=41532

  • AspNetWebTools2013_1Setup.exe @ ~39MB
  • WebToolsExtensionsVS.msi (for Visual Studio 2012 standard edition[s]) @ ~21MB
  • WebToolsExtensionsVWD.msi (for Visual Studio 2012 free "Web" edition[s]) @ ~21MB
  • AspNetWebTools2013_1Setup.exe @ ~39MB
  • WebToolsExtensionsVS.msi(适用于 Visual Studio 2012标准版[s])@~21MB
  • WebToolsExtensionsVWD.msi(适用于 Visual Studio 2012免费“Web”版[s])@~21MB

回答by flipchart

This answerclaims that VS2012 can't understand Razor 3, so you (and me) will have to upgrade to VS2013

这个答案声称 VS2012 无法理解 Razor 3,所以你(和我)将不得不升级到 VS2013

回答by Kna?is

I am currently using a workaround to get the IntelliSense working in VS2012 (I assume that this would not work properly a) if I was using VS2013 for the same project; b) if I tried using Razor 3 features):

我目前正在使用一种解决方法来让 IntelliSense 在 VS2012 中工作(我认为这不会正常工作 a)如果我将 VS2013 用于同一个项目;b) 如果我尝试使用 Razor 3 功能):

Change the web.config to use old webpagesversion:

更改 web.config 以使用旧webpages版本:

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

To make sure that this does not impact the production in any way I added a web.config transformation:

为了确保这不会以任何方式影响生产,我添加了一个 web.config 转换:

<add key="webpages:Version" value="3.0.0.0" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />

Unfortunately this workaround has issues (that I have noticed so far):

不幸的是,这个解决方法有问题(我到目前为止已经注意到了):

  • Extension methods (like Html.RenderPartial()are not recognized and show up as error in the designer).
  • 扩展方法(例如Html.RenderPartial()无法识别并在设计器中显示为错误)。

回答by Kna?is

I got this answer from Microsoft developer:

我从微软开发人员那里得到了这个答案:

MVC 5/Razor 3 tooling support for VS 2012 has not shipped yet. We hope to ship this support in Mid November.

对 VS 2012 的 MVC 5/Razor 3 工具支持尚未发布。我们希望在 11 月中旬提供此支持。

So the accepted answer is true for now but we can plan to soon have this support in VS2012 as well.

所以目前公认的答案是正确的,但我们可以计划很快在 VS2012 中也提供这种支持。

回答by hatsrumandcode

ASP.NET and Web Tools 2013.1 for Visual Studio 2012 has now been released and should resolved the MVC5 intellisense issue with VS 2012.

Visual Studio 2012 的 ASP.NET 和 Web Tools 2013.1 现已发布,应该可以解决 VS 2012 的 MVC5 智能感知问题。

This release brings a ton of great improvements, and include some fantastic enhancements to ASP.NET MVC 5, Web API 2, Scaffolding and Entity Framework to users of Visual Studio 2012 and Visual Studio 2012 Express for Web.

此版本带来了大量重大改进,包括对 Visual Studio 2012 和 Visual Studio 2012 Express for Web 用户的 ASP.NET MVC 5、Web API 2、脚手架和实体框架的一些出色增强。

You can download the update: http://blogs.msdn.com/b/webdev/archive/2013/11/18/announcing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012.aspx

您可以下载更新:http: //blogs.msdn.com/b/webdev/archive/2013/11/18/annoucing-release-of-asp-net-and-web-tools-2013-1-for-视觉工作室-2012.aspx

To Moderator: It might be just a download link, but this resolve the issue, it worked for me and it worked for the users that voted this as answer. If you are going to delete it please provide a reason, thanks!

对主持人:这可能只是一个下载链接,但这解决了问题,它对我有用,对将其投票为答案的用户也有用。如需删除请说明原因,谢谢!

回答by chsword

install the "ASP.NET and WebTools 2013.1 for Visual Studio 2012" in the Microsoft Platform Installer

在 Microsoft 平台安装程序中安装“ASP.NET and WebTools 2013.1 for Visual Studio 2012”

回答by Ryan Griffith

For me, my issue was that I had my build files being output to a directory other than bin. Once my files we're output to bin, then my syntax highlighting came back.

对我来说,我的问题是我的构建文件被输出到bin. 一旦我的文件输出到bin,我的语法高亮就回来了。

One way to fix this is to create a post-build action which copies the files in the project-rooted bin folder (vs a filter outside of the project) to your target directory.

解决此问题的一种方法是创建一个构建后操作,将项目根目录中的 bin 文件夹(与项目外部的过滤器)中的文件复制到您的目标目录。

The following article explains: http://www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/

以下文章解释:http: //www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/