The ASPX See Motor is the legacy view engine crafted into ASP.Web MVC from its initial times. The Razor See Motor is a lot more sophisticated and is now the default view engine of ASP.Web Main MVC. This report compares these two view engines in temporary and then discusses how you can operate with the Razor See Motor in ASP.Web Main MVC.
To operate with the code examples furnished in this report, you ought to have Visual Studio 2019 set up in your procedure. If you really do not already have a duplicate, you can down load Visual Studio 2019 right here.
Produce an ASP.Web Main MVC challenge in Visual Studio
1st off, let us develop an ASP.Web Main challenge in Visual Studio 2019. Next these actions will develop a new ASP.Web Main MVC 5 challenge in Visual Studio 2019.
- Launch the Visual Studio IDE.
- Click on on “Create new challenge.”
- In the “Create new project” window, pick out “ASP.Web Main Website Application (Design-See-Controller)” from the checklist of templates shown.
- Click on Future.
- In the “Configure your new project” window, specify the title and locale for the new challenge.
- Optionally examine the “Place option and challenge in the similar directory” examine box, dependent on your choices.
- Click on Future.
- In the “Additional Information” window demonstrated future, pick out .Web 5. as the concentrate on framework from the drop-down checklist at the top rated. Leave the “Authentication Type” as “None” (default).
- Be certain that the examine boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Razor runtime compilation” are unchecked as we won’t be applying any of individuals features right here.
- Click on Produce.
A new ASP.Web Main MVC 5 challenge will be produced. We’ll use this challenge to operate with Razor sights in the subsequent sections of this report.
What is a view engine?
A view engine translates a server-facet template into HTML markup and renders it in the web browser when induced by a controller’s motion method. ASP.Web MVC to begin with shipped with the ASPX See Motor, but the Razor See Motor was added in afterwards variations. The Razor See Motor is now the default view engine for ASP.Web Main MVC purposes.
While the ASPX See Motor is accessible as section of the Procedure.Website.Mvc.WebFormViewEngine namespace, the Razor See Motor is accessible in the Microsoft.AspNetCore.Mvc.Razor namespace.
How does a view engine operate?
Just about every view engine contains three components: the ViewEngine class, the view class, and the template parser. The ViewEngine class extends the IViewEngine interface and implements its customers. This class is liable for locating view templates. The view class extends the IView interface and implements its customers. This class is liable for combining the template with information and then changing it to HTML markup to be rendered in the web browser. The template parser is a parsing engine that compiles the view into executable code.
You can also develop your possess personalized view engine in ASP.Web Main. To do this, you develop a class that extends the IView and the IViewEngine interfaces pertaining to the Microsoft.AspNetCore.Mvc.ViewEngines namespace. You then put into practice the two solutions of the IViewEngine interface, specifically GetView and FindView. You also put into practice the RenderAsync method of the IView interface. This method is liable for rendering the view engine at runtime.
Produce a new Razor view in ASP.Web Main MVC
In the new ASP.Web Main MVC software we produced previously mentioned, let’s develop a easy view. To do this, edit the HomeController.cs file and insert the subsequent code:
community IActionResult Welcome()
ViewData["Concept"] = "Good day Earth!"
Future, develop a new view file named Welcome.cshtml in the Views/Home folder and enter the subsequent code:
Eliminate the default view engines in ASP.Web Main MVC
When you develop a personalized view engine, you might usually want to take away the default view engines. You can take away both equally the Razor See Motor and the ASPX See Motor and then insert your possess personalized view engine as demonstrated in the code snippet supplied down below.
Use an if assemble in Razor See Motor
In this area we’ll look at how we can system our view applying the Razor syntax. Let us to start with use some typical constructs these types of as the if, if else, and switch situation statements.
The subsequent code snippet illustrates how you can use an if statement in Razor.
@var x = 10
@if (x > 5)
The price of x is greater than 5.
The future code snippet exhibits how you can use an if else statement in Razor.
@var x = 2
@if (x > 5)
The price of x is greater than 5.
The price of x is much less than 5.
Use a switch situation statement in Razor See Motor
Here is how you can use a switch situation statement in Razor.
var textual content=string.Vacant
textual content="This is the to start with functioning day of the 7 days."
textual content="This is the last functioning day of the 7 days"
textual content="Now is: " + weekday
If the day is a Monday, when you operate the software you would see the subsequent output in your web browser.
Use loops in Razor See Motor
You can use loops in your Razor sights to conduct repetitive actions. The subsequent code snippet illustrates how you can operate with loops in Razor.
Exhibiting figures 1 to 10
@for(var i = 1 i <= 10 i++)
You can just take benefit of foreach loops when functioning with collections. The subsequent code snippet illustrates how you can screen all keys pertaining to the Request.Headers collection.
@foreach (var k in this.Context.Request.Headers)
If you want to use a model in the view, you ought to develop a model class as demonstrated in the code snippet supplied down below.
community class Creator
community int Id get established
community string FirstName get established
community string LastName get established
To make things easy, develop the model class in the Products option folder. You can use this model in the view as illustrated in the code snippet supplied right here:
- Creator Id: @Design.Id
- 1st Identify: @Design.FirstName
- LastName: @Design.LastName
The Razor See Motor is a lot more sophisticated than its previously counterpart, offering a friendlier syntax for producing HTML code from templates. Take note that Razor is a normal purpose templating engine — you can use it everywhere to render HTML.
You can also operate with third occasion view engines, these types of as Spark, SharpDOM, and NDjango, in ASP.Web Main MVC. I’ll show how to develop a personalized view engine in ASP.Web Main MVC in a afterwards write-up right here.