51

Currently my razor view engine throws and error saying:

Please use language version 6 or higher.

That may just be resharper giving me a pointer. But how do I make razor use C# 6.0. Rest of my solution in the cs files I can use all the new version 7 features.

RBT
  • 24,161
  • 21
  • 159
  • 240
MoXplod
  • 3,813
  • 6
  • 36
  • 46
  • 2
    possible duplicate of [C# 6.0 Features Not Working with Visual Studio 2015](http://stackoverflow.com/questions/27968963/c-sharp-6-0-features-not-working-with-visual-studio-2015) – cadrell0 Jul 31 '15 at 00:45
  • no - that solution did not fix this problem. – MoXplod Oct 01 '15 at 17:47

5 Answers5

30

I believe this is due to a bug in the templates for web.config when the project is upgraded to a newer version of the .net framework.

I was able to fix this by going in to web.config, finding the system.codedom node, and changing the content to look like this:

<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>
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • 15
    Microsoft.Net.Compilers and Microsoft.CodeDom.Providers.DotNetCompilerPlatform must be installed in order to work – Anytoe Feb 07 '17 at 16:30
  • 3
    (Re-)Installing the above two packages will add this section to the .config file. – Alen Siljak May 31 '17 at 15:34
  • 6
    Yes, thanks @Alen, and I found that just installing Nuget pkg for Microsoft.CodeDom.Providers.DotNetCompilerPlatform is enough since Microsoft.Net.Compilers is a dependency. – PBMe_HikeIt Nov 06 '17 at 19:07
  • 2
    [DotNetCompilerPlatform v3.6.0](https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/3.6.0) no longer has a dependency on `Microsoft.Net.Compilers` – Eric D. Johnson Mar 25 '21 at 21:05
20

If you are running this from a 4.5.1 project, upgrading to 4.5.2 might fix it.

Otherwise, it is recommended to install this NuGet package to provide the new functionalities: Microsoft.CodeDom.Providers.DotNetCompilerPlatform

So doing something like this:

<p>@Model.Person?.Name</p>

Might work. If it doesn't, try being explicit like this:

<p>@(Model.Person?.Name)</p>
Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
12

Other answers are quite good, but I found a good and short article that definitively clarify the steps for this issue: https://cpratt.co/using-csharp-6-or-7-with-mvc-5/

In short:

  1. In the Package Manager Console install Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

  2. checka/alters you Web.config with the following lines at the end:

<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:15 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>
Christian Wattengård
  • 5,543
  • 5
  • 30
  • 43
Gianpiero
  • 3,349
  • 1
  • 29
  • 42
4

The best step-by-step instructions I could find for enable C# 6 features in RazorEngine templates is here: https://github.com/Antaris/RazorEngine/issues/363#issuecomment-273106183

What's not mentioned in there is to make sure when adding the compilers section to your app.config, that the version number matches the version from the package you are installing. In the comment it was version 1.0.3, but the package I installed had 1.0.4 instead, so it needed to look like this:

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

mscrivo
  • 1,107
  • 12
  • 14
0

Create a folder in project's solutions with this name

Directory.Build.props

Then add this dependency

<Project>
 <PropertyGroup>
  <LangVersion>6.0</LangVersion>
 </PropertyGroup>
</Project>
Anurag
  • 43
  • 6