How to Use C# 6 with Web Site Project Type

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 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.

How to use C# 6.0 or 7.0 in an old ASP.NET Website (not Website Project)

If you install the Microsoft.CodeDom.Providers.DotNetCompilerPlatform it will update your web.config file to use the current version - right now it's on 7.3:

  <system.codedom>
<compilers>
<compiler extension=".cs" language="c#;cs;csharp" warningLevel="4"
compilerOptions="/langversion:7.3 /nowarn:1659;1699;1701;612;618"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform,
Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4"
compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\"Web\" /optionInfer+"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform,
Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</compilers>
</system.codedom>

How to use newer language features in an ASP.net website?

Changing the Target Framework to 4.8 should get you C# 7.3 features. C# features are standardized and correspond with the framework version.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

Is there a way to use C# 6 or higher in .NET 4?

The language-version only relies on the version of Visual Studio. To use features intorduced in C#6 you need at least VS2015. This has nothing to do with the installed .NET-framework-version. In fact you can use VS2015 and its compiler-features (e.g. auto-implemented properties with an initial value) and compile against .Net 2.0:

int MyProperty { get; set; } = -1;

To change the target-framework (e.g. .Net 2.0) use Project properties-->Application-->Target framework.

To change the language version via Project properties-->Build-->Advanced-->Language version.

Let´s consider the following example: Beginning with C#3 you can write the following:

var a = new { MyProperty = 1 };

This compiles for any .Net-version. However the most common use-case for anonymous types is when using Linq to fetch data from a database. Linq was introuced in framework-version 3.5 and heavily depends on lambda-expressions and extension-methods, which both were introduced in C#3:

var result = myCollection.Select(x => new { MyProperty = x.MyProperty });

So although you could use anonymous types in earlier versions of the framework, there was little need to do so.

To get a better view on the difference between the C#- (=language)-version and the framework-version read this post. This thread on the other side lists the language-versions and in which version of VS they were released.

Project Type GUID for ASP.NET Website project

If we have known it is a web site project, we can refer to the list in the following link to find the correspond project type GUID.

Visual Studio Project Type GUIDs

For example:

Web Site    {E24C65DC-7377-472B-9ABA-BC803B73C61A}

I create a web project and open the sln file, I find they are the same.

Sample Image

Sample Image

adding a web app project to my solution, and the other projects in the solution already use .net framework, can I use .net core for the new project?

It all depends on the APIs you use in your application. If you only use APIs included in .NET Standard then no problem to use them in an application under .NET 6. Otherwise I join Kirk Woll to migrate your code under .NET 6 especially since the unification of the platform. I think it will remain only .NET 6 aka .NET core in the future.

How to use C# 7 within Web Application ASPX code-before pages?

Finally, I solved it.

With the help of Sysinternals Process Monitor I discovered that csc.exe tries to load a file called "csc.exe.config" which was not present.

So I copied the "csc.exe.config" file from my local website's "bin\roslyn" folder into my live website's "bin\roslyn" folder.

After that, everything works as expected.

Just to be safe, I also copied "csi.exe.config" the same way, although it seems that this is not required, at least in my tests.

(The reason that the config files were initially not present is that my publish scripts usually intentionally ignore configuration files to not overwrite existing configurations).



Related Topics



Leave a reply



Submit