Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null propagation feature and Razor views

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

like image 613
Julien Avatar asked May 03 '17 10:05

Julien


1 Answers

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());
like image 111
Patrick Hofman Avatar answered Nov 18 '22 03:11

Patrick Hofman