How to use Razor View Engine in ASP.NET Core MVC
The ASPX Perspective Engine is the legacy watch engine built into ASP.Internet MVC from its original times. The Razor Perspective Engine is more highly developed and is now the default watch engine of ASP.Internet Core MVC. This posting compares these two watch engines in transient and then discusses how you can perform with the Razor Perspective Engine in ASP.Internet Core MVC.
To perform with the code examples supplied in this posting, you should have Visible Studio 2019 installed in your process. If you don’t presently have a duplicate, you can download Visible Studio 2019 listed here.
Build an ASP.Internet Core MVC challenge in Visible Studio
1st off, let’s create an ASP.Internet Core challenge in Visible Studio 2019. Adhering to these methods will create a new ASP.Internet Core MVC five challenge in Visible Studio 2019.
- Launch the Visible Studio IDE.
- Click on “Create new challenge.”
- In the “Create new project” window, select “ASP.Internet Core World wide web App (Product-Perspective-Controller)” from the checklist of templates exhibited.
- Click Future.
- In the “Configure your new project” window, specify the title and site for the new challenge.
- Optionally check the “Place remedy and challenge in the very same directory” check box, based on your preferences.
- Click Future.
- In the “Additional Information” window demonstrated next, select .Internet five. as the target framework from the drop-down checklist at the top rated. Leave the “Authentication Type” as “None” (default).
- Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Razor runtime compilation” are unchecked as we will not be employing any of those characteristics listed here.
- Click Build.
A new ASP.Internet Core MVC five challenge will be designed. We’ll use this challenge to perform with Razor sights in the subsequent sections of this posting.
What is a watch engine?
A watch engine interprets a server-facet template into HTML markup and renders it in the website browser when brought on by a controller’s motion process. ASP.Internet MVC originally delivered with the ASPX Perspective Engine, but the Razor Perspective Engine was included in afterwards variations. The Razor Perspective Engine is now the default watch engine for ASP.Internet Core MVC purposes.
Even though the ASPX Perspective Engine is offered as element of the Procedure.World wide web.Mvc.WebFormViewEngine namespace, the Razor Perspective Engine is offered in the Microsoft.AspNetCore.Mvc.Razor namespace.
How does a watch engine perform?
Each and every watch engine includes three parts: the ViewEngine class, the watch class, and the template parser. The ViewEngine class extends the IViewEngine interface and implements its members. This class is responsible for finding watch templates. The watch class extends the IView interface and implements its members. This class is responsible for combining the template with details and then converting it to HTML markup to be rendered in the website browser. The template parser is a parsing engine that compiles the watch into executable code.
You can also create your individual customized watch engine in ASP.Internet Core. To do this, you create a class that extends the IView and the IViewEngine interfaces pertaining to the Microsoft.AspNetCore.Mvc.ViewEngines namespace. You then carry out the two strategies of the IViewEngine interface, namely GetView and FindView. You also carry out the RenderAsync process of the IView interface. This process is responsible for rendering the watch engine at runtime.
Build a new Razor watch in ASP.Internet Core MVC
In the new ASP.Internet Core MVC application we designed over, let’s create a easy watch. To do this, edit the HomeController.cs file and incorporate the pursuing code:
public IActionResult Welcome()
ViewData["Concept"] = "Howdy Entire world!"
return Perspective()
Future, create a new watch file named Welcome.cshtml in the Views/Dwelling folder and enter the pursuing code:
@ViewData["Concept"]
Get rid of the default watch engines in ASP.Internet Core MVC
When you create a customized watch engine, you could often want to get rid of the default watch engines. You can get rid of both equally the Razor Perspective Engine and the ASPX Perspective Engine and then incorporate your individual customized watch engine as demonstrated in the code snippet provided below.
companies.AddMvc()
.AddViewOptions(selections =>
selections.ViewEngines.Obvious()
selections.ViewEngines.Add(typeof(MyCustomViewEngine))
)
Use an if assemble in Razor Perspective Engine
In this section we’ll examine how we can program our watch employing the Razor syntax. Let us first use some popular constructs this kind of as the if, if else, and swap circumstance statements.
The pursuing code snippet illustrates how you can use an if assertion in Razor.
@var x = 10
@if (x > five)
The worth of x is higher than five.
The next code snippet exhibits how you can use an if else assertion in Razor.
@var x = two
@if (x > five)
The worth of x is higher than five.
else
The worth of x is less than five.
Use a swap circumstance assertion in Razor Perspective Engine
Below is how you can use a swap circumstance assertion in Razor.
@
var weekday=DateTime.Now.DayOfWeek.ToString()
var textual content=string.Empty
@swap(weekday)
circumstance "Monday":
textual content="This is the first working working day of the week."
split
circumstance "Friday":
textual content="This is the last working working day of the week"
split
default:
textual content="Today is: " + weekday
split@textual content
If the working day is a Monday, when you operate the application you would see the pursuing output in your website browser.
Use loops in Razor Perspective Engine
You can use loops in your Razor sights to carry out repetitive steps. The pursuing code snippet illustrates how you can perform with loops in Razor.
Displaying numbers 1 to 10
@for(var i = 1 i <= 10 i++)
@i
You can just take gain of foreach loops when working with collections. The pursuing code snippet illustrates how you can screen all keys pertaining to the Request.Headers collection.
- @k.Critical
@foreach (var k in this.Context.Request.Headers)
If you want to use a model in the watch, you should create a model class as demonstrated in the code snippet provided below.
public class Author
public int Id get established
public string FirstName get established
public string LastName get established
To make matters easy, create the model class in the Types remedy folder. You can use this model in the watch as illustrated in the code snippet provided listed here:
@model Author
- Author Id: @Product.Id
- 1st Title: @Product.FirstName
- LastName: @Product.LastName
The Razor Perspective Engine is more highly developed than its previously counterpart, giving a friendlier syntax for creating HTML code from templates. Observe that Razor is a standard function templating engine — you can use it wherever to render HTML.
You can also perform with 3rd occasion watch engines, this kind of as Spark, SharpDOM, and NDjango, in ASP.Internet Core MVC. I’ll exhibit how to create a customized watch engine in ASP.Internet Core MVC in a afterwards post listed here.
Copyright © 2021 IDG Communications, Inc.