How to make razor view engine to use c# 6.0

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>

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>

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>

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>


Comments

  1. Leon

    • 2017/6/7

    NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not 

  2. Brixton

    • 2021/2/1

    How to make razor view engine to use c# 6.0. Ask Question Asked 6 years, 1 month ago. Active 1 year, 10 months ago. Viewed 13k times 48 5. Currently my razor view

  3. Braxton

    • 2018/12/10

    If you new with creating an application in ASP.NET MVC 5 then go with following links. Create ASP.NET MVC Application using Visual Studio 2017.

  4. Murphy

    • 2020/1/9

    Razor is a templating engine and ASP.NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not have any ties with ASP.NET MVC. Now, Razor Syntax is compact which minimizes the characters to be used, however it is also easy to learn.

  5. Rhys

    • 2018/1/21

    Introduction to ASP.NET Web Programming Using the Razor Syntax (C#). 02/07/2014; 40 minutes to read You create a new variable using the var keyword.

  6. Taylor

    • 2021/2/15

    We’ve seen how to use razor view engine in our views. If you want to write C# multiple statements or single statement then you need to declare the razor with curly brackets. @ {} And if you want to use this variable inline in any text or in HTML then you need to write the first razor then you’ll see the IntelliSense.

  7. Antonio

    • 2016/7/28

    NET MVC to use Razor but not duplicate the code base. Other uses of Razor have yet to be developed and/or announced, but template languages in general have 

  8. Ayaan

    • 2021/1/15

    Right-click on the Index action and select Add View… Select Empty from the Template dropdown and click Add button. Visual Studio will create an Index.cshtml file inside the View/Home folder. Notice that Razor view has a cshtml extension. If you're building your MVC application using Visual Basic it will be a VBHTML extension.

  9. Benjamin

    • 2015/3/19

    MVC 4. Web pages with Razor syntax have the special file extension cshtml (Razor with C#) or can use Razor and or ASPX view engine in our application.

  10. Martino

    • 2018/5/8

    using; while; C# Razor keywords must be double-escaped with @(@C# Razor Keyword) (for example, @(@case)). The first @ escapes the Razor parser. The second @ escapes the C# parser. Reserved keywords not used by Razor. class; Inspect the Razor C# class generated for a view. The Razor SDK handles compilation of Razor files.

  11. Mohammad

    • 2018/7/20

    You can see that the view engine is smart about where the HTML ends and the C sharp begins, and vice versa. For example, it doesn't have a problem with the 

  12. Rossetti

    • 2015/5/25

    As we saw above, the Razor view engine that comes with ASP.NET uses markup files with the extension .cshtml. The Razor syntax uses the @ symbol to transition to C# and evaluate expressions.

  13. Ariel

    • 2017/6/22

    For example, you can use HTML Helpers to render standard HTML tags like Engine is Razor syntax are easy to learn and much clean than Web Form syntax.

  14. Conner

    • 2018/5/22

    The ASPX View Engine is the legacy view engine built into ASP.NET MVC from its initial days. The Razor View Engine is more advanced and is now the default view engine of ASP.NET Core MVC.

Comments are closed.

Recent Posts