简体   繁体   English

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

[英]VS2017 - C#7 language features not working in MVC views

Using an interpolated string in a .cshtml view is giving the following intellisense error: Feature 'interpolated strings' is not used in C#5. Please use language version 6 or greater. 在.cshtml视图中使用插值字符串会产生以下智能感知错误: Feature 'interpolated strings' is not used in C#5. Please use language version 6 or greater. Feature 'interpolated strings' is not used in C#5. Please use language version 6 or greater. This and other C#7 language features are working in compiled code (.cs files). 这个和其他C#7语言功能在编译代码(.cs文件)中工作。

As you can see below, the latest major version is C#7. 如下所示,最新的主要版本 C#7。

在此输入图像描述

According to a comment in this question , "default" means "latest major version" . 根据这个问题的评论, “默认”是指“最新主要版本”

So why the error? 那么为什么会出错呢? Also, why doesn't it show "Latest Major Version" and "Latest Minor Version" as separate list options, as I have seen in many online examples? 另外,为什么它不会将“最新主要版本”和“最新次要版本”显示为单独的列表选项,正如我在许多在线示例中看到的那样?

UPDATE: 更新:

I finally managed to get string interpolation working in my views by installing the CodeDom providers package (The Microsoft.Net.Compilers package is related to Msbuild. The CodeDOM Providers package is related to ASP.NET and other apis that compile at runtime hence why cshtml files will show errors if this is not installed even if the latest language version is selected for msbuild). 我终于通过安装CodeDom提供程序包 (Microsoft.Net.Compilers包与Msbuild相关)设法在我的视图中进行字符串插值.CodeDOM Providers包与ASP.NET和其他在运行时编译的apis有关,因此为什么要使用cshtml如果没有安装,即使为msbuild选择了最新的语言版本,文件也会显示错误。 This automatically added the following to my web.config: 这会自动将以下内容添加到我的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>

Ref: C# 6.0 Features Not Working with Visual Studio 2015 参考: C#6.0功能不适用于Visual Studio 2015

Upgrading to MVC6 would also have fixed it I believe. 我相信升级到MVC6也会修复它。

I suspect the key to your question is when you say "in a view". 我怀疑你的问题的关键是当你说“在一个视图中”。 Assuming this is in the context of ASP.Net, you should look at your web.config, which may specify it's own LangVersion setting (likely hardcoded to 5 in your case). 假设这是在ASP.Net的上下文中,您应该查看您的web.config,它可以指定它自己的LangVersion设置(在您的情况下可能硬编码为5)。

More details on configuring ASP.Net to use newer versions of C# can be found in this Roslyn documentation issue . 有关配置ASP.Net以使用更新版本的C#的更多详细信息,请参阅此Roslyn文档问题

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

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