Enabling c# 7 in a asp.net application
Solution 1
In website's NuGet window:
- Uninstall
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
- Re-install it
- In
web.config
, under:system.codedom > compilers > compiler
, changecompilerOptions
="/langversion:6 to 7
Solution 2
I am able to compile it with default language setting but not with C# 7 option.
But below setting gives compile time error:
so you can keep your language version setting as default.
If you experimenting with Roslyn and not using Visual 2017 default compiler build then you may need to make some more changes
Select your project name and right click >> Properties Window >> Build and then add the below two options in "Conditional Compilation symbols" text box __DEMO__,__DEMO_EXPERIMENTAL__
Update
In order to use C# 7.0, you need to use 2.0+ version of Microsoft.Net.Compilers
after installing the latest version of Microsoft.Net.Compilers (2.0+) you can select the language version as C# 7.
so the best solution is to install the latest version of Microsoft.Net.Compilers (2.0+).
Solution 3
For C# 7.x support set the Build Configuration Language Version of project to C# latest minor version (latest)
Build Configuration Language Version
If you are using CodeDOM Providers for .NET Compiler Platform ("Roslyn") (e.g. the Microsoft.CodeDom.Providers.DotNetCompilerPlatform nuget package) set compilerOptions="/langversion:latest" in web.config for asp.net.
<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:latest /nowarn:1659;1699;1701"/>
</compilers>
</system.codedom>
For more info:
- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/langversion-compiler-option
- https://github.com/aspnet/RoslynCodeDomProvider#what-is-changed
Solution 4
If you try to install Microsoft.CodeDom.Providers.DotNetCompilerPlatform
version 2.0.0 and your project targets a version of .net older than 4.6, then it will automatically use an older version of roslyn which only supports up to langversion 6. This is because newer versions of roslyn, including the first version to support csharp-7, require at least .net-4.6 to run. If your project targets an older version of .net, you will get the error message you see:
CS1617 Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.
- Ensure that your project is targeting at least .net-4.6. Retarget if necessary.
- If your project still uses
packages.config
, then you must uninstall and reinstallMicrosoft.CodeDom.Providers.DotNetCompilerPlatform
to update your project file to point to the .net-4.6 variant of the nuget package. If you are using<PackageReference/>
, you are all set (but you have to manually configureweb.config
’ssystem.codedom
section).
Solution 5
You need to replace files in your project folder
/Bin/roslyn
with files from NuGet package folder
/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0/tools/Roslyn472
csv.exe in project folder not replaced during installation a new version of nuget. After replacing files it works excellent.
And do not forget to change .Net version to latest in project properties.
davidthegrey
World traveler, classic musician, photographer. Programming is a big part of earning my daily caviar. C# in asp.net is what I use most, along with t-sql, client side javascript (+ jquery and bootstrap), classic asp, old vb6. Starting with Vue.
Updated on September 20, 2020Comments
-
davidthegrey over 3 years
I just started working on my old solution in Visual Studio 2017. Just opening the solution in the old IDE worked seamlessly. The c# application projects now default to the c# 7.0 compiler. The property pages of those project (compilation/advanced) let easily chose the targeted language version of the compiler, defaulting to the latest.
I cannot find a way to enable c# 7.0 in the asp.net web projects though. If I write a statement such as:
if (int.TryParse("1", out int myInt)) { ... }
the IDE warns me saying that I need to use version 7+ of the language.
My research on this topic shows I should target the specific c# version in the system.codedom compilers area of the web.config file, so to target the newest Roslyn version.
What I have now is:
<system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
which targets c# 6. What are the correct settings for c# 7, provided that I have already downloaded the latest Roslyn with nuget?
Update Here is a screenshot of the available Compile options for a web project (it is Italian VS2017 but it should be easy to understand). No possibility to select the targeted c# version there.