How to use C# 6 with Web Site project type?
Solution 1
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 NuGet package for Roslyn CodeDOM.
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
should add the DLL files...
PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
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.
...and also 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 the XML is still missing, try adding yourself.
Solution 2
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.
Update.
VS 2017 and 2019 have this feature moved to Build -> ASP.NET Compilation.
Solution 3
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.
Solution 4
Possible solutions, taken from the comments on ScottGu's blog posting (search for "8026" on the page):
Solution suggestion 1 (David Taylor)
Add these RTM code dom elements to 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>
Then add the Roslyn and Microsoft.CodeDom.Providers.*.dll stuff into your BIN directory.
It is easy enough to setup, but just was strange to me that the default template wasn't set up for it if you selected .NET 4.6 when creating a new "Web Site" in VS 2015 RTM.
Solution suggestion 2 (Martin)
Further to David Taylor's comment above, it looks like the system.codedom settings are correct when a Web App is created with the TargetFramework as the default v4.5.2. Changing the TargetFramework to v4.6 appears to modify the compiler settings for CSharp, in a way that causes an issue.
My workarounds was as follows:
- File/New/ASP.NET Web Application
- Select "Web API" template from ASP.NET 4.5.2 Templates
- Take a copy of the system.codedom element (and its contents) in web.config
- Using Properties/TargetFramework, set the Target Framework to 4.6
- Replace the modified system.codedom element in web.config with the copy taken prior to changing TargetFramework
- Hit F5
Home Page should load as expected.
For info, the system.codedom contents immediately after changing TargetFramework to v4.6. was as follows (note use of the Type "Microsoft.CSharp.CSharpCodeProvider"):
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
<providerOption name="CompilerVersion" value="v4.0"/>
</compiler>
<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>
Solution 5
This is not for a Web Site Project. This is how you do it for an ASP.NET MVC project.
You can toggle C# 6 in the Visual Studio UI. This is such an easy option, it is worth trying first.
- Right click your project and select Properties.
- Click the Build tab.
- The build tab has an Advanced... button at the very bottom.
- This opens up the Advanced Build Settings as shown below. Select C# 6.0.
Uwe Keim
German developer. Some of my apps: SharePoint Systemhaus Göppingen (zwischen Stuttgart und Ulm) Eigene Homepage erstellen Test Management Software Windows 10 Ereignisanzeige Very proud father of Felix (2012) and Ina (2014). Loves running, climbing and Indian food. Code Project member #234.
Updated on January 27, 2022Comments
-
Uwe Keim over 2 years
Updated an existing Web Site project type Visual Studio 2015, I changed the Framework to 4.6.
I then expected to have all those new features available in my code behind files.
Unfortunately I'm getting errors like:
Error CS8026: Feature 'expression-bodied property' is not available in C# 5. Please use language version 6 or greater.
or e.g.:
Error CS8026: Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.
I did a quick Google check and found a guy posting some comments in a blog posting of ScottGu (search for "8026" on the page).
Since I do not understand his solution, plus I want to have the solution more visible, I've created this SO posting.
My question:
How can I have a Visual Studio 2015 project of type Web Site (i.e. not Web Application) to recognize C# 6 features?
-
jbtule almost 9 yearsIn regards to your solution 2,
Microsoft.CSharp.CSharpCodeProvider
version 4.0 means it's not using Rosyln, Thus will not work for C# 6.0. -
Uwe Keim almost 9 yearsThis works as expected. C# 6 now available. Drawback is that my App_GlobalResources/Resources.resx do not generate the auto-class to strongly-typed access the resources anymore.
-
snumpy almost 9 yearsThis also allows for use of C# 6 features in front-end files in WebForms.
-
Doug Morrow over 8 yearsAs a note, I was able to build without errors after project target was changed to 4.6. However, I needed to install this package to resolve red squigglies showing up from intellisense.
-
Stefan Anghel over 8 yearsI'm using this in a WebForms WebSite (no project file). Sadly, simple .aspx changes trigger a painfully slower recompilation when re-requesting the page in a browser. Maybe Roslyn is theoretically faster for a WebApplication (with proj file), but for WebSites it seems to be significantly slower via csc in the App_Code folder... Anyone seeing the same thing?
-
Admin over 8 yearsNote, for WebForms to compile with C#6 you need to install NuGet packages
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
and its dependencyMicrosoft.Net.Compilers
-
mac over 8 yearsNote that if you have more than one web site in your solution - things wont work, the easiest way to reproduce is create empty solution and start adding projects into it, after second web site - you will start receive "cannot find csc.exe" errors
-
Saul over 8 yearsWhen migrating from an older solution, make sure
<LangVersion>
in thecsproj
file is equal to6
. It can cause problems if it's something else. -
costa over 7 years@StefanAnghel: Did you solve the issue you were having? I am in the same boat as you are, i.e. I have a website, and after seeing your message I am reluctant to enable this feature.
-
costa over 7 yearsFrom a deployment perspective, do you need to deploy anything else other than the output from the publish process? Thx
-
Stefan Anghel over 7 years@costa Ended up reverting to C# 5, I so SO hate not having string interpolation, but then again, maybe using a WebSite project in 2016 is not such a great ALM choice... I blamed it on management, I hope you have someone to blame it on too...
-
costa over 7 years@StefanAnghel: Thank you for getting back to me. The best thing would be to upgrade the app from website to Web application but I didn't have the time. It's a big application. Anyway, good luck!
-
RickNZ over 7 yearsIn addition to adding the suggested package, I had to update Microsoft.Net.Compilers to v2.0.1 in order for C# 6 to start working on Web Forms pages (this is with VS 2017 RTM).
-
RokX about 7 yearsI had to add the compiler section also in the web.config located in the Views folder.
-
Jess almost 7 years@Manuzor you might be right. This was for a ASP.NET MVC project.
-
Navid Golforoushan over 6 yearsI have a .net 2.o project and worked for me. actually, I knew the solution but I forget it. tanks for solution 100 likes ;)
-
b_levitt almost 6 yearsI appreciate the crop job @Uwe but I don't think I agree with removing the rest of the top nav since context is lost for where this particular menu item should be.
-
Jess over 5 years@Ucho what is wrong? Is it an MVC project? Do you have the Advanced option in the build settings? Are you missing options for C# language version? Did you set the version and not able to use language features?
-
Yarl over 5 years@Jess: Ah, is probably working. I did not notice that my error come from IntelliSense not from build.
-
Chris over 4 yearsThis setting doesn't seem to apply to code in aspx pages, e.g.
<% Eval(whatever) %>
. -
Vertigo over 4 yearsLatest VS 2017 has moved this to 'Build -> ASP.NET Compilation'. See my updated answer.
-
Denny Jacob over 4 yearsFor web site project types I tested @b_levitt's answer on VS2019. (1) Make sure you have a web.config. If not add one. (2) Save the solution by selecting it on the Solution Explorer and issuing [Ctrl] + [S]. (3) Select the project on the Solution Explorer and change the Target Framework to 4.7.2 (4) Select "Enable latest C#..." option from menu Build > ASP.NET Compilation.
-
Corgalore over 4 yearsI opened an older folder-based website project in VS2019 and had to File > Save the project before that option would show up.
-
Jason Honingford about 4 yearsWhen deploying, the server gives me "This program is blocked by group policy. For more information, contact your system administrator" Even tried setting trust to full.
-
Allen about 4 yearsAll the web.config examples referenc e1.0.0, but the web.config section should reference 2.0.1, right? That is what nuget added to my web .config.... e.g. ... Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
-
IrishChieftain over 3 yearsNot available in website template
-
IrishChieftain over 3 years"The path is not of a legal form"
-
Michael over 2 yearsDidn't work for my MVC project. The dropdown is disabled, and hyperlink underneath says I can't change versions.
-
Michael over 2 years...But for reasons I cannot understand, it builds anyway. Even though the IDE complains it has no idea how to compile null-coalescing operators.