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

时间:2017-10-05 14:32:02

标签: c# asp.net-mvc visual-studio visual-studio-2017 c#-7.0

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

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

enter image description here

根据this question中的评论,“默认”表示“最新主要版本”

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

更新:

我终于设法通过安装CodeDom providers package在我的视图中使用字符串插值(Microsoft.Net.Compilers包与Msbuild相关.CodeDOM Providers包与ASP.NET和其他编译的apis相关在运行时因此,即使为msbuild选择了最新语言版本,如果未安装此错误,cshtml文件也会显示错误。这会自动将以下内容添加到我的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>

参考:C# 6.0 Features Not Working with Visual Studio 2015

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

1 个答案:

答案 0 :(得分:4)

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

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

相关问题