11

Hello I have a strange issue.

I use the null propagation feature in my razor pages like this

@(Model.ligneDossierLie?.dossier_id)

my project is based on 4.6.1 Framework and I use the last codeDom compiler 1.0.4 and compiler 2.1.0

In the razor view I have an error message saying that I can't use a C# 6 feature with C#5.

But my project is using c#7 ....

I roll back to c#6 and it works fine.

Is someone know how to use c#7 in this case?

Thank you.

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.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
    </compilers>
  </system.codedom>

compilerOptions="/langversion:6

was set on 7 and I roll back to 6. After that I didn't had the error anymore, My views are compiling and working correctly

Julien
  • 411
  • 4
  • 13
  • Maybe [my similar question](http://stackoverflow.com/q/42700380/107625) (including [the answer](http://stackoverflow.com/a/42712883/107625)) helps? – Uwe Keim May 03 '17 at 10:17
  • We use [StackExchange.Precompilation](https://github.com/StackExchange/StackExchange.Precompilation) for this; among the tools are bits that let you use C# vLatest in razor - like this: https://github.com/StackExchange/StackExchange.Precompilation/blob/fd536b764983e2674a4549b7be6f26e971190c1e/Test.WebApp/Global.asax.cs#L32 – Marc Gravell May 03 '17 at 10:19
  • @UweKeim I seen your question, but it's not the problem in my case. Thank you – Julien May 03 '17 at 10:25
  • @MarcGravell Actualy there's no standard way to use c#7 and razor engine with full features? – Julien May 03 '17 at 10:26
  • I just installed the StackExchange precompiler, and my project doesn't start anymore. csc error. – Julien May 03 '17 at 11:00
  • I had this problem this morning after updating CodeDom from 1.0.3 to 1.0.4 and the compiler from 2.0.1 to 2.1.0. I got the same error message in my Razor views, but it would compile just fine. I rolled back to the previous versions (1.0.3 and 2.0.1) and the error messages went away. Not the best solution, but I'll probably stick with the older versions until I can figure out how to upgrade without introducing those errors. – brady May 03 '17 at 16:58

1 Answers1

11

It seems you can use C# 6 and C# 7 features indeed, although it needs a little work.

Add these packages:

  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform (1.0.4 currently)
  • Microsoft.Net.Compilers (2.1.0 currently)

Make sure you have this section in 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.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:7 /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.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>

And the required imports in your csproj file (should be there automatically, but check it):

<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.4\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.4\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.2.1.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.2.1.0\build\Microsoft.Net.Compilers.props')" />

And this target in it:

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> ... </Target>

Previous answer:

You can't use that since the ASP.NET MVC Razor view engine doesn't use Roslyn to compile your views. Hence it can't use features available from C# 6 and higher (the null propagation operator = C# 6).

You have to write your code in the pre-C# 6 style, or use another package to take advantage of an alternative view engine, like the one Stack Exchange created (thanks for Marc Gravell to point to that): https://github.com/StackExchange/StackExchange.Precompilation.

According to their documentation, you have to include the package:

Install-Package StackExchange.Precompilation.Build -Pre

and then put this at the end of your Application_Start in Global.asax.cs:

ViewEngines.Engines.Add(new RoslynRazorViewEngine());
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I don't understand. Razor isn't using Roslyn to copile even if The last version of compiler and codeDom is installed? In this cas could you tell me why it works with c#6 and doesn't with c#7 ? Thanks – Julien May 03 '17 at 10:29
  • No, it does not work for C# 6. For 5 it does. It uses the old `csc` C# compiler. – Patrick Hofman May 03 '17 at 10:29
  • Strange, I'm currently using null propagation feature so c#6 feature in my views and it works fine, but not with c#7. I'll try your solution, but it's strange. – Julien May 03 '17 at 10:36
  • Where do you set it to be C# 6 then? – Patrick Hofman May 03 '17 at 10:37
  • @Julien Cool. It works here. Are you sure you have the latest versions of `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` (1.0.4) and `Microsoft.Net.Compilers` (2.1.0)? (Check the actual packages) – Patrick Hofman May 03 '17 at 11:19
  • Yes I have both. I uninstalled stackExchange precompiler and now everything is fine with c#7 but I still get the error in visual studio even if the page compiles correctly. – Julien May 03 '17 at 11:23
  • That might be a problem with the VS support of ASP.NET MVC then. @Julien All C# 7 features work fine here. – Patrick Hofman May 03 '17 at 11:27
  • I think so. It works fine here too now. Maybe there was some mess in packages. Thanks – Julien May 03 '17 at 11:30
  • Okay, updated my answer to make sure all steps are there. – Patrick Hofman May 03 '17 at 11:32
  • I didn't need to install `Microsoft.Net.Compilers`. Just adding the latest `DotNetCompilerPlatform` and setting `/langversion:7` worked for me. – wensveen Feb 01 '19 at 11:04