Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 907
Kev Avatar asked Oct 05 '17 14:10

Kev


People also ask

What version of C++ does VS2017 support?

We've updated the C++ compiler and standard library in this release with enhanced support for C++11 and C++14 features.

Does Visual Studio support C language?

The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn't support Microsoft C++, but does support . NET languages and cross-platform development.

Is Visual Studio 2017 still available?

Visual Studio 2017: mainstream support ends April 12, 2022, and the product will transition to extended support until April 2027. During extended support we'll provide fixes only for security issues. We recommend users move to the 15.9 supported baseline to remain under support.

Does VS2017 support .NET 5?

NET5 in future versions. VS2017 does not support it.


1 Answers

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.

like image 196
Julien Couvreur Avatar answered Sep 30 '22 17:09

Julien Couvreur