In this article, you will learn the following points about Model in MVC 5.
- What is a View Engine in ASP.NET MVC?
- Why is View Engine required in ASP.NET MVC 5?
- What is Razor Engine in MVC 5?.
- What is Razor Syntax in MVC 5?
- Comments in razor syntax in MVC 5
- Escape sequence in Razor syntax
- Interview Question
Following are the previews to two articles on ASP.NET MVC 5.
What is a View Engine in ASP.NET MVC?
View Engine is responsible for rendering the view into Html form to the browser. ASP.NET MVC supports web form(.aspx) and Razor View (.cshtml).
View Engine is used to convert html+Programming language to pure HTML.
As per the above diagram, you can see that a view can contain C# and HTML code. Once the view renders on the browser, the C# code written on the view is converted into pure HTML format. To convert C# code into pure HTML is the job of ViewEngine. You can cross verify by inspecting the browser and verifying the C# data on the inspected browser; you can not find the C# code on the browser.
Why is View Engine required in ASP.NET MVC 5?
- View engine is responsible for creating HTML for View
- It converts html+Programming language to pure HTML
- It is used to find the corresponding view for the action method.
- It is used to find a View from the shared folder.
- We used to write C#/VB code on View
- View Engine is required to implement the strongly-typed View.
What is Razor Engine In MVC 5?
- Razor Engine is an advanced view engine.
- This is not a new language but it is a new markup syntax.
- The namespace for Razor Engine is System.Web.Razor.
- View file extension is .cshtml or .vbhtml (partial and layout view) based on language.
- Razor syntax is easy to learn and much cleaner than Web Form syntax.
- Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks)
- Razor Engine does not support design mode in VS means you can not see your view page look and feel.
- Razor Engine supports TDD. It does not depend on System.Web.UI.Page class.
What is Razor Syntax in MVC 5?
Two types of Razor syntax,
- Single statement block: It starts with @.
- Multi statement block: It starts with @{..............}
It must be always enclosed in @{ ... }
The semicolon “;” must be used to end statements
Inline expressions (variables and functions) start with @
Example of Single block
A single statement block is used when a single line of code needs to write on View.
Example
To display current DateTime.
Create Index.cshtml View and add following code. If you new with creating an application in ASP.NET MVC 5 then go with following links.
- @{
- ViewBag.Title = "Index";
- }
- <hr />
- <div>
- <label>Current Date : @DateTime.Now.ToString()</label><br />
- <label>Current Long Date: @DateTime.Now.ToLongDateString()</label><br />
- <label>Addition of Numbers: @(250 + 250)</label>
- </div>
Inspect browser and search “DateTime.Now.ToString()” on browser; we cannot find the C# code on the browser as follows.
If you inspect browser and search “DateTime.Now.ToString()” on the browser, it could not see the C# code on the browser; we can only see the pure Html code. This is the job of View Engine to convert C# code into pure Html format on the browser.
Example of Multi statement block
In multi-statement block, we can write more than one line of code as follows.
- @{
- var num1 = 100;
- var num2 = 200;
- var num3 = num1 + num2;
- string message = "Welcome to ASP.NET MVC 5 Tutorials.";
- }
- <label>Addition of two Num is = @num3</label><br />
- <label>@message</label>
Comments in razor syntax in MVC 5
Two type of comments,
Single Line
Example: // code.........
Multi-Line
Example: /* code........ */
Single Line comment Code,
Multi-Line comment Code,
Escape sequence in Razor syntax in MVC 5
Razor syntax always starts with “@” then how can we use “@” symbol on the browser?
If you want to print your Twitter account on the browser then we need the help of escape sequence. If we want to print “@shrimant_vt” then we need to write double at the rate symbol like “@@shrimant_vt”.
Example
My Twitter Account = @shrimant_vt
First, we will not use an escape sequence in razor syntax to display the Twitter account.
Code
- <label>My Twitter Account = @shrimant_vt</label>
After executing the application you will get the following error message.
Compiler Error Message
CS0103: The name 'shrimant_vt' does not exist in the current context
Output
To avoid the above compiler error message, you need to use escape sequence in Razor syntax as below.
- <label>My Twitter Account = @@shrimant_vt</label>
Interview Question on Razor View Engine In ASP.NET MVC 5,
- What is Razor View Engine in MVC?
- Why View Engine is Required in ASP.NET MVC 5?
- What is Razor?.
- What is Razor Syntax?
- Use of Escape sequence in Razor view engine.
I hope you understand the concepts of Razor View Engine in ASP.NET MVC 5. If you like this article, then please share it to help other people to increase their knowledge.
Thanks for reading.