VS2017 - C#7语言功能在MVC视图中不起作用

Kev*_*Kev 7 c# asp.net-mvc visual-studio c#-7.0 visual-studio-2017

在.cshtml视图中使用插值字符串会产生以下智能感知错误:Feature 'interpolated strings' is not used in C#5. Please use language version 6 or greater.此语言和其他C#7语言功能在编译代码(.cs文件)中有效.

如下所示,最新的主要版本 C#7.

在此输入图像描述

根据这个问题的评论,"默认"是指"最新主要版本".

那么为什么会出错呢?另外,为什么它不会将"最新主要版本"和"最新次要版本"显示为单独的列表选项,正如我在许多在线示例中看到的那样?

更新:

我终于通过安装CodeDom提供程序包(Microsoft.Net.Compilers包与Msbuild相关)设法在我的视图中进行字符串插值.CodeDOM Providers包与ASP.NET和其他在运行时编译的apis有关,因此为什么要使用cshtml如果没有安装,即使为msbuild选择了最新的语言版本,文件也会显示错误.这会自动将以下内容添加到我的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.7.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.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
Run Code Online (Sandbox Code Playgroud)

参考:C#6.0功能不适用于Visual Studio 2015

我相信升级到MVC6也会修复它.

Jul*_*eur 4

我怀疑你问题的关键是当你说“在观点中”时。假设这是在 ASP.Net 的上下文中,您应该查看您的 web.config,它可能指定它自己的 LangVersion 设置(在您的情况下可能硬编码为 5)。

有关配置 ASP.Net 以使用较新版本的 C# 的更多详细信息,请参阅此Roslyn 文档问题