Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.

Wednesday, September 18, 2019

Razor View Engine In ASP.NET MVC 5

We will learn Razor View Engine in MVC 5 and why view engine is required in MVC 5. Also, we will learn about Razor View Engine syntax in MVC 5 with examples.
Razor View Engine In ASP.NET MVC 5

In this article, you will learn the following points about Razor View Engine in MVC 5.

  • What is a View Engine in ASP.NET MVC?
  • Why View Engine is 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 two articles on ASP.NET MVC 5

What is a View Engine in ASP.NET MVC 5?

  • View Engine is responsible for rendering the view into Html form to the browser. ASP.NET MVC 5 support Razor View(.cshtml).
  • View Engine is used to converts html+Programming language to pure Html.
Razor View Engine In ASP.NET MVC 5



As per the above diagram you can see 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 converting C# code into pure Html this is the job of ViewEngine. You can cross verify by inspecting browser and verify the C# data on inspected browser you could not found the C# code on the browser.


Why View Engine is Required in ASP.NET MVC 5?

  • View engine is responsible for creating Html for view
  • It converts html+Programming language to pure Html
  • View engine is used to find the corresponding view for the action method.
  • View engine is used to find a view from the shared folder.
  • View engine is required to write C#/VB code on view
  • View Engine is required to implement 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 Visual Studio means you can not see your view page look and feel.
  • Razor Engine support 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
Multi statement block must be always enclosed in @{ ... }
The semicolon “;” must be used to ending statements
Inline expressions (variables and functions) start with @

Example of Single statement  block:

A single statement block is used to use only 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: @(500+250)</label>
</div>

Output:
Razor View Engine In ASP.NET MVC 5


Inspect browser and search “DateTime.Now.ToString()” on browser we can not found the C# code on the browser.

Razor View Engine In ASP.NET MVC 5



If you inspect browser and search “DateTime.Now.ToString()” on the browser you could not see the C# code on 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 the multi-statement block, we can write more than one line of code as follow.
<hr />
@{ 
    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>


Output:
Razor View Engine In ASP.NET MVC 5
Comments in razor syntax in MVC 5

Two types of comments
  • Single Line:
Example: // code.........
  • Multi-Line:
Example: /* code........ */


Single Line comment Code

//var num1 = 100;
Multi-Line comment Code
 /*var num1 = 100;
    var num2 = 200;
    var num3 = num1 + num2;
    string message = "Welcome to ASP.NET MVC 5 Tutorials.";*/


Escape sequence in Razor syntax in MVC 5


Razor syntax always starts with “@” then how can we print “@” symbol on the browser.
If you want to print your twitter account on browser then we need to take 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 the escape sequence in razor syntax to display the twitter account. 
Code:
<label>My Twitter Account = @shrimant_vt</label>


After executing application you will get following error message.

Compiler Error Message: CS0103: The name 'shrimant_vt' does not exist in the current context

Output:

Razor View Engine In ASP.NET MVC 5


To avoid the above compiler error message you need to use escape sequence in razor syntax as follow.

Code:
<label>My Twitter Account = @@shrimant_vt</label>


Output:
Razor View Engine In ASP.NET MVC 5

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.


Related Articles:

References: 


I hope you understand the concepts of Razor View Engine in ASP.NET MVC 5. If you like this article then please share this article that will help other people to increase there knowledge.

Thanks for reading.

Don't Forget To:   
 More detail watch follow video on Razor View Engine In ASP.NET MVC 5:

Share:

0 comments:

Post a Comment

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave