Description
Opening this issue for discussion.
The fixes for ASP.NET and ASP.NET Core are described below. They are related, but Core involves an extra step.
I don't know how hard it would be to automate this:
- add/update NuGet package (probably depends on enabling AddNuget fixer first)
- fix web.config
- add code to startups (for Razor in ASP.NET Core)
Based on StackOverflow issues, this is a fairly common problem people run into.
FYI @Pilchie @kuhlenh @CyrusNajmabadi for discussion.
Consuming C# compiler from ASP.NET
A number of people on stackoverflow are reporting trouble adopting C# 7.0 in their ASP.NET and ASP.NET Core projects. This often has strange symptoms, where the IDE accepts the new syntax (no squiggles), but building the solution or F5 produce errors in the Output window.
This is caused by a mismatch between the built-in compiler (used by VS) and the compiler pulled in as a nuget package by ASP.NET projects. The Microsoft.CodeDom.Providers.DotNetCompilerPlatform
package pulls in the Microsoft.Net.Compilers
package, which overwrites what compiler will be used to build the solution.
To troubleshoot this, open the "Manage NuGet packages" UI, then select the "Browse" tab. Search for "Microsoft.Net.Compilers" and look at the installed version and the newest available version.
Versions 1.x of the package correspond to C# 6.0. You need version 2.x for C# 7.0.
The fix for ASP.NET:
- Install the latest
Microsoft.Net.Compilers
package (see versioning information about compiler packages to help you figure out which one you need) - Look for "LangVersion" in your
web.config
and change it to the version you want (for example7.0
)
The fix for ASP.NET Core:
- Install the
Microsoft.CodeAnalysis.CSharp
(version 2.0.0) packages - In
Startup.cs
, in theConfigureServices
method, configure Razor to use C# 7.0 by doing the following:
services.AddMvc().AddRazorOptions(options =>
options.ParseOptions = new CSharpParseOptions(LanguageVersion.CSharp7));
Note: if you want to use C# 7.0 tuples in your project, you'll also need to install System.ValueTuple
(version 4.3.0).
Update: Ryan from ASP.NET Core mentioned that Razor templates will soon pick up the LangVersion from the project, instead of being specified separately. If that happens, we shouldn't need special UpgradeProject logic for ASP.NET Core. I'm not sure about ASP.NET.