Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.

最后都变了- 提交于 2019-12-19 12:52:47

问题


The following line does not compile when I put in a Razor View.

var extPropLookupNameCompania = $"extension_{SettingsHelper.ClientId.Replace("-", "")}_{"Compania"}";

However in the controller the same line works perfectly fine.

Why I cant user string interpolation on the razor views? or Maybe I need to configure something?


回答1:


If you are experiencing this error in a .NET Framework 4.5.1 project, upgrading to 4.5.2 solves the problem.




回答2:


You have to encapsulate it with braces like this:

EDIT: I updated because there was a missing curly.

var extPropLookupNameCompania = $("{extension_{SettingsHelper.ClientId.Replace("-", "")}_{"Compania"}");

I tried the following successfully:

 @($"It is {DateTime.Now}")



回答3:


The key is to see the compilerOptions="/langversion:6 portion of the following in your web.config. Without it I got errors trying to use string interpolation inside a .cshtml file.

  <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=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>


来源:https://stackoverflow.com/questions/31844056/feature-interpolated-strings-is-not-available-in-c-sharp-5-please-use-languag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!