22

I've updated my ASP.NET Mvc 5 web application to use the new c# 8.0 features through Visual Studio 2019 and everything works fine until I try to use these new features inside a Razor view.

For example, if I try to use the new switch expression:

@{
    ViewBag.Title = "About";

    var foo = 1;
    var bar = foo switch
    {
        1 => "one",
        2 => "two",
        _ => string.Empty
    };
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Use this area to provide additional information.</p>

The compiler won't complain until I try to reach the page, giving me a compilation error.

Compilation error

I suspect that Microsoft.CodeDom.Providers.DotNetCompilerPlatform must be updated but it seems that there is no update available.

Is there any way to use c# 8.0 language features in Razor views?

dbraillon
  • 1,742
  • 2
  • 22
  • 34
  • will this help? - https://stackoverflow.com/questions/31689374/how-to-make-razor-view-engine-to-use-c-sharp-6-0 – yob Nov 07 '19 at 15:33
  • well, that is exactly where I found that it is probably related to `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` package but I can't make it work for c# 8.0 somehow – dbraillon Nov 08 '19 at 09:58
  • works for me, - I updated reference to Microsoft.CodeDom.Providers.DotNetCompilerPlatform to version # 2.0.1 (Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 2.0.1) – yob Nov 10 '19 at 12:55
  • 1
    I just tried another time with a new solution: create a new solution with default mvc template with .Net Framework, write some c# 8.0 code, visual studio ask if I want to update my project to use c# 8.0, accept it then write that very same code to a view. It does compile but won't execute at runtime... – dbraillon Nov 12 '19 at 12:06
  • 1
    Any luck on this issue? – eaglei22 Nov 13 '19 at 15:36
  • Nothing so far but if I am not the only one, I will start a bounty to make it more visible I guess... – dbraillon Nov 14 '19 at 09:29
  • Does Visual Studio convert your .NET Framework project to a .NET Core project when updating to support C# 8.0? Because a .NET Framework project cannot use C# 8.0, it has to be .NET Standard 2.1 (e. g. .NET Core 3.0) – mu88 Nov 14 '19 at 11:33
  • Absolutely not, it only does one thing, put this line inside my csproj file: `8.0`. – dbraillon Nov 14 '19 at 15:56
  • Regarding to [this question](https://stackoverflow.com/questions/56651472/does-c-sharp-8-support-the-net-framework) .Net Framework can only use some of c# 8.0 functionalities, that is why the new switch expression works. I guess, it would never be applied to Razor view as it is not officially supported in the first place... – dbraillon Nov 14 '19 at 15:59
  • @dbraillon I'm using the `LangVersion` trick in my .net 472 projects and your code compiles fine. And as you said: for some reason this is not used to compile the razor code. I've also upgraded the `DotNetCompilerPlatform 2.0.0` to `2.0.1` to see if that would solve your problem. But alas. I also changed the following line in `Web.config`: `compilerOptions="/langversion:default` to `compilerOptions="/langversion:latest` and to `compilerOptions="/langversion:8.0`. But alas. I'm sorry, I haven't been able to find a solution yet. – Thomas Heijtink Nov 17 '19 at 09:00
  • Setting `LangVersion` affects the compiler used to compile your project but not the runtime compiler that is used to compile the Razor views. So in the end, you won’t be able to use these features in Razor on ASP.NET (non-Core). – poke Nov 20 '19 at 10:14
  • @poke yeah that's what I figured out also, the only way would be that `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` gets updated to support some of these functionalities right? – dbraillon Nov 20 '19 at 10:17
  • @dbraillon *Maybe* (not sure if that would be enough) but you can be pretty sure that this won’t happen since C# 8 for .NET Framework is not fully supported by design. – poke Nov 20 '19 at 10:21

2 Answers2

6

.net framework supports C# 7.3 that's why you can't make your Razor View work

.net core 3 supports C# 8 and i was able to make your example work with a .net Core 3 MVC app.

You can have a look here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

I hope the above helps :)

Ricky Stam
  • 2,116
  • 21
  • 25
  • 2
    While what you are saying is true. I **am** able to use C# 8.0 features in my .net 4.7.2 project by simply changing the `LangVersion` in my csproj. I can even use the nullable feature. And the suggested code by the OP compiles and runs just fine. So it's interesting why this wouldn't work for an ASP.NET application. – Thomas Heijtink Nov 17 '19 at 08:53
  • 1
    @ThomasHeijtink, see last update for [Microsoft.CodeDom.Providers.DotNetCompilerPlatform](https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform) was an year ago - 13.09.2018. So, now C# 8 features is not supported with bundled compiler. – hal Nov 18 '19 at 19:20
  • 1
    @ThomasHeijtink, you could contribute [to repository](https://github.com/aspnet/RoslynCodeDomProvider). I believe you just need to use last version of [Microsoft.Net.Compilers](https://www.nuget.org/packages/Microsoft.Net.Compilers) in [RoslynCodeProvider.settings.targets](https://github.com/aspnet/RoslynCodeDomProvider/blob/master/tools/RoslynCodeProvider.settings.targets): `3.3.1`. You could build your own nuget package from sources and use it (or make PR and get feedback with hopes to merge it into master). – hal Nov 18 '19 at 19:20
  • Has anyone attempted a fork? I don't see one on GitHub (that adds this change). My goal is to gradually transition an ASP.NET Classic project to core, and having some code stuck on 7.x kind of hurts that. – Sören Kuklau Apr 16 '20 at 12:34
6

OK so, there are some things that needs to be clarified first.

According to this answer C# language version is tied with the framework, and C# 8.0 is fully available to all language that support .Net Standard 2.1. .NET Framework 4.7.2 and below does not support .NET Standard 2.1 and it will not be the case in the future.

In the meantime there is a way to use C# 8.0 if you specify the LangVersion in any .csproj project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net48</TargetFrameworks>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

This will enable C# 8.0 and some non-tied framework features will be available to you. Check the link answer to know which one.

Finally to answer my own question, at this time, no, there is no way to use any of the C# 8.0 features inside a Razor view. The run-time compilation seems to be done with a package named Microsoft.CodeDom.Providers.DotNetCompilerPlatform so the only way would be to update that package to allow some of the new features to be used.

I will update that answer as soon as I get more information.

Sam Rueby
  • 5,914
  • 6
  • 36
  • 52
dbraillon
  • 1,742
  • 2
  • 22
  • 34