Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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=&quot;Web&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

If it's still missing. Try adding yourself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...