当我尝试在我的 cshtml
文件中使用一个简单的内插字符串时,我遇到了这个恼人的错误:
@for (int i = 0; i < ppTitles.Count; i++)
{
<p>@ResourceLibrary.Resources.GetString($"PP_Text_{i + 1}")</p>
}
Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.
我已经尝试了所有的解决方案here
- 在
web.config
中获取了langversion - 尝试用
@()
包围字符串 - 将项目升级到
4.5.2
和here
- 将语言版本从
default
更改为6
- 使用
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
安装 CodeDome
我在项目的其他区域(在常规 .cs
文件中)使用了字符串插值,没有任何问题。
这是怎么回事?
最佳答案
当使用 C#
5 或更早版本时,内插字符串不可用,您需要安装 NuGet 包才能获得支持。请注意(作为错误状态)这是开箱即用的 C#
6 及更高版本。
您需要将 NuGet 包 Microsoft.CodeDom.Providers.DotNetCompilerPlatform
安装到您的 Web 项目(或任何包含您的 View 的项目)
这个包将添加对内插字符串的支持(以及提高编译性能)。如果包安装没有将以下内容添加到您的 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=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
关于c# - 无法在 cshtml 中使用内插字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46315659/