Kev
Kev

Reputation: 2726

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. This and other C#7 language features are working in compiled code (.cs files).

As you can see below, the latest major version is C#7.

enter image description here

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). This automatically added the following to my 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

Upgrading to MVC6 would also have fixed it I believe.

Upvotes: 7

Views: 1683

Answers (1)

Julien Couvreur
Julien Couvreur

Reputation: 4993

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).

More details on configuring ASP.Net to use newer versions of C# can be found in this Roslyn documentation issue.

Upvotes: 4

Related Questions