How to use C# 6 with Web Site project type? How to use C# 6 with Web Site project type? asp.net asp.net

How to use C# 6 with Web Site project type?


I've tested this with ASP.NET MVC 5 (tested 5.2.3), and your mileage may vary with other web frameworks, but you just need to add the Roslyn CodeDOM| NuGet package

CodeDOM Providers for .NET Compiler...

Replacement CodeDOM providers that use the new .NET Compiler Platform ("Roslyn") compiler as a service APIs. This provides support for new language features in systems using CodeDOM (e.g. ASP.NET runtime compilation) as well as improving the compilation performance of these systems.

PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

The NuGet package should add the DLL files and add the following to your 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.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=\"Web\" /optionInfer+" />    </compilers>  </system.codedom>

If it's still missing. Try adding yourself.


See also this blog post from Sayed Ibrahim Hashimi on how to do this through the VS IDE.

In Visual Studio 2015 Update 1 we have included a new feature to simplify this. When you have a solution open which has at least one web project which is targetting .NET 4.5+ and does not have the DotNetCompilerPlatform NuGet package in the Project menu you’ll see a new option, Enable C# 6 / VB 14 appear.
enter image description here

Update.

VS 2017 and 2019 have this feature moved to Build -> ASP.NET Compilation.enter image description here


I installed the DotNetCompilerPlatform as @jbtule suggested, but was still getting the same errors.

PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

I closed the solution, deleted the bin and obj folders, then opened the solution and rebuilt. Now the C# 6 features work.