Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make razor view engine to use c# 6.0

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.

like image 795
MoXplod Avatar asked Jul 29 '15 00:07

MoXplod


People also ask

What is Razor C#?

Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or VB.NET programming languages. Razor was in development in June 2010 and was released for Microsoft Visual Studio 2010 in January 2011. Razor is a simple-syntax view engine and was released as part of MVC 3 and the WebMatrix tool set.

How do you make a razor view?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK.

What language is Cshtml?

cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser. This server side coding is similar to the standard ASP.NET page enabling dynamic web content creation on the fly as the webpage is written to the browser.

What is Razor View engine in MVC?

The Razor View Engine is the default View Engine for the ASP.NET Core apps. It looks for Razor markup in the View File and parses it and produces the HTML response. The Controller in MVC invokes the View by passing the data to render. The Views must have the ability to process the data and generate a response.

Can razor be used for MVC?

Note that Razor is a general purpose templating engine — you can use it anywhere to render HTML. You can also work with third party view engines, such as Spark, SharpDOM, and NDjango, in ASP.NET Core MVC. I’ll demonstrate how to create a custom view engine in ASP.NET Core MVC in a later post here.

Why do we need razor engine?

Because razor engine is already intelligent enough, it already knows that if razor comes between the text then the text should be printed on the screen as it is. We’ve seen how to use razor view engine in our views.

How to use comments in Razor View engine?

Razor View Engine has two types of comments, one is single-line and another is multiline. Razor uses the syntax "@* ..*@" for the comment block but in a C# code block, we can also use /* */ or //.


2 Answers

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> 
like image 170
Bradley Uffner Avatar answered Sep 20 '22 12:09

Bradley Uffner


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> 
like image 30
Maxime Rouiller Avatar answered Sep 17 '22 12:09

Maxime Rouiller