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

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

在 .cshtml View 中使用内插字符串会产生以下智能感知错误:C#5 中未使用“内插字符串”功能。请使用语言版本 6 或更高版本。此功能和其他 C#7 语言功能在编译代码(.cs 文件)中有效。

正如您在下面看到的,最新的主要版本 C#7。

enter image description here

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

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

更新:

我终于通过安装 CodeDom providers package 使字符串插值在我的 View 中起作用(Microsoft.Net.Compilers 包与 Msbuild 有关。CodeDOM Providers 包与 ASP.NET 和其他在运行时编译的 api 有关,因此即使选择了最新的语言版本,如果未安装,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>

引用:C# 6.0 Features Not Working with Visual Studio 2015

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

最佳答案

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

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

关于c# - VS2017 - C#7 语言功能在 MVC View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588244/

相关文章:

c# - 在代码中保存 SQL Server 登录 (C#)

c# - 如何找到特定行的行号?

c# - 使用 ScriptService 时为 "Google.GoogleApiException: Google.Apis.Requests.RequestError Request is missing required authentication credential"

css - LESS 不在 Windows 2012 服务器上编译?

c# - Nhibernate 拦截器 - 获取属性长度 OnSave

asp.net - 将 System.Web.HttpContext.Current 转换为 System.Web.HttpContextBase

windows - Spectre/Meltdown 补丁导致 COM 方法返回 E_ACCESSDENIED

c# - 在 C# 中使用类型作为函数参数创建通用对象

c# - 每次构建时,其中一个程序集都会从我的解决方案中删除

visual-studio - Visual Studio 指南 : Whats the right way since its 'outdated' ?