简体   繁体   English

如何使剃刀视图引擎使用 C# 6.0

[英]How to make razor view engine to use C# 6.0

Currently my razor view engine throws and error saying:目前我的剃刀视图引擎抛出并错误说:

Please use language version 6 or higher.请使用语言版本 6 或更高版本。

That may just be resharper giving me a pointer.这可能只是给我一个指针。 But how do I make razor use C# 6.0.但是如何让剃须刀使用 C# 6.0。 Rest of my solution in the cs files I can use all the new version 7 features.我在 cs 文件中的其余解决方案可以使用所有新版本 7 的功能。

I believe this is due to a bug in the templates for web.config when the project is upgraded to a newer version of the .net framework.我相信这是由于项目升级到 .net 框架的较新版本时web.config模板中的错误所致。

I was able to fix this by going in to web.config , finding the system.codedom node, and changing the content to look like this:我可以通过进入web.config来解决这个问题,找到system.codedom节点,然后将内容更改为如下所示:

<compilers>
  <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>

If you are running this from a 4.5.1 project, upgrading to 4.5.2 might fix it.如果您从4.5.1项目运行它,升级到4.5.2可能会修复它。

Otherwise, it is recommended to install this NuGet package to provide the new functionalities: Microsoft.CodeDom.Providers.DotNetCompilerPlatform否则,建议安装此 NuGet 包以提供新功能: Microsoft.CodeDom.Providers.DotNetCompilerPlatform

So doing something like this:所以做这样的事情:

<p>@Model.Person?.Name</p>

Might work.可能会奏效。 If it doesn't, try being explicit like this:如果没有,请尝试像这样明确:

<p>@(Model.Person?.Name)</p>

Other answers are quite good, but I found a good and short article that definitively clarify the steps for this issue: https://cpratt.co/using-csharp-6-or-7-with-mvc-5/其他答案都很好,但我发现了一篇很好的简短文章,明确阐明了这个问题的步骤: https ://cpratt.co/using-csharp-6-or-7-with-mvc-5/

In short:简而言之:

  1. In the Package Manager Console install Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform在包管理器控制台中安装Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

  2. checka/alters you Web.config with the following lines at the end:检查/更改您的 Web.config,并在末尾添加以下几行:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp"
      extension=".cs"
      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4"
      compilerOptions="/langversion:7 /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript"
      extension=".vb"
      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4"
      compilerOptions="/langversion:15 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>

The best step-by-step instructions I could find for enable C# 6 features in RazorEngine templates is here: https://github.com/Antaris/RazorEngine/issues/363#issuecomment-273106183我可以在 RazorEngine 模板中找到启用 C# 6 功能的最佳分步说明: https ://github.com/Antaris/RazorEngine/issues/363#issuecomment-273106183

What's not mentioned in there is to make sure when adding the compilers section to your app.config, that the version number matches the version from the package you are installing.没有提到的是确保在将编译器部分添加到您的 app.config 时,版本号与您正在安装的软件包中的版本相匹配。 In the comment it was version 1.0.3, but the package I installed had 1.0.4 instead, so it needed to look like this:在评论中它是 1.0.3 版本,但我安装的包有 1.0.4,所以它需要看起来像这样:

<system.codedom>
<compilers>
  <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
</compilers>

Create a folder in project's solutions with this name在项目的解决方案中使用此名称创建一个文件夹

Directory.Build.props

Then add this dependency然后添加这个依赖

<Project>
 <PropertyGroup>
  <LangVersion>6.0</LangVersion>
 </PropertyGroup>
</Project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM