Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

What is Razor View Engine in ASP.Net Core?


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

Razor-View-Engine-in-ASP-Net-Core

Table of Content

What is View Engine?

View Engine is responsible for producing an HTML response when called by the Controller Action method.The Controller Action methods can return different types of responseswhich are collectively called as Action Results. The View-result is the Action Result which produces the HTML Response.

The View Results are produced by the View Engine. When the Controller Action method call the view () or PartialView ()it calls the View Engine, which generate the HTML Response.View Engine is responsible for rendering the view into HTML form to the browser, View Engine is used to convert HTMLProgramming language to pure HTML.

Why is View Engine required in ASP.NET?

  1. View engine is responsible in creating HTML for View.
  2. It converts Pure HTML programming language to HTML
  3. View Engine is used to find the Related view for the action method.
  4. We are able to write C#/VB code on View
  5. View Engine is Needed to implement the strongly-typed View.

What is Razor View Engine?

The Razor View Engine is the default View Engine for the ASP.NET Core. It looks for Razor markup in the View File and parses it and produces the HTML response.

Razor Markup

The Controller in MVC invokes the View by passing the info to render. The Views must have the power to process the info and generate a response. This is done by the Razor markup, which allows us to use C# code in an HTML file. The Razor View Engine process these markups to get the HTML.

The files which contains Razor markup have a. cshtml file extension.The Razor syntax is shorter, simpler and straightforward to find out because it uses the C# or visual basic. The Visual Studio Intelligence support also helps with Razor syntax.

What is Razor Syntax in MVC?

There aretwo types of Razor syntax.

  1. Single statement block: It starts with @.
  2. Multi statement block: It is starts with @ {Coding part}

It needs to be always enclosed in @ {Coding part}

The semicolon “;” must be used to end the statements

Inline expressions (variables and functions) start with @

   

1) Single block Statements

A single statement block is used when a single line of code needs to write on View.

Example

To display current Date-time.

Create Index.cshtml View and add below code.


@{  
    ViewBag.Title = "Index";  
}<div>  
   <label>Current-Date: @DateTime.Now.ToString()</label>
  
   <label>Current-Long Date: @DateTime.Now.ToLongDateString()
</label></div> 

Inspect browser and search for “DateTime.Now.ToString()” on browser.We cannot find the C# code on the browser as we did.

If you inspect browser and search for “DateTime.Now.ToString()” on the browser. it could not see the C# code on the browser. We can only see the only HTML code. This is the job of View Engine which convert C# code into pure Html format in the browser.

 

2) Multi statement block

We can also define a multiline statement block as a single-line statement block. In a multiline statement block we can define multiple code statements and doprocess. A multiline block is between opening and closing curly braces {}, but the opening brace will have the "@" character in the same line if we define the "@" and opening curly braces in different lines then it will give an error.

Example:

<div>
@{
	Var Java=100;
	Var Rubi=200;
	Var Ionic=400;
	Var sum=(Java+Rubi+Ionic);
  }
Sum of @Java and @Rubi and @Ionic is</div>

Now, let’s see how to write Razor code:

 
Variables

// Using the var keyword:  
var a = &amp;quot;Welcome to MyProgram&amp;quot;;  
var b = 300;  
var c = DateTime.Today;    
// Using data types:  
string greeting = &amp;quot;Welcome to MyProgram &amp;quot;;  
int counter = 300;  
DateTime day = DateTime.Today; 

Now, in the above example, we have declared variables using “var” keyword as well as throughits types forexample. string, int, Date Time. You can declare any of them. Italso uses int, float, decimal, bool and string data types.

 
Conditions

If statement

It starts with code block and its condition is written in parenthesis and the code which needs to be executed once condition gets true is written inside braces.

Let’s understand with the below example.


@{var a=60;}<html><body>
      @if (a&gt;50)  
       {<p>The a is greater than 50.</p>
       }</body></html>

In the above example, we have declared var “a” has a value of 60 and we are using if statement which is validating value of “a”. If the value is greater than 50 then the code written in side braces is executed.

 

If – Else statement

Initially the code block and its condition arewritten in parenthesis and code which is needs to be executed once the condition gets true is written inside braces and if it does not gettrue then code write inside else block gets executed.

Let’s understand with the below example.


@ {var a=60;}<html><body>
@if (a&gt;50)  
    {<p>The value is greater than 50.</p>
	}  
else  
	{
<p>The value is less than 50.</p>
	}</body></html>

In the above example, we have declared var “a” having a value of 60 and we are using if statement which is validating value of “a”. If the “a” is greater than 50 then the code written inside braces gets executed else the “a” is less than 50 and it will be executed.

 

Switch statement

It is used to check multiple times with different conditions.


@ {  
var month = DateTime.Now.Year.ToString();  
    var message = &amp;quot;&amp;quot;;  
}<html><body>
  @ {  
        switch (month)   
        {  
            case &amp;quot;January&amp;quot;:  
                message = &amp;quot;Starting of Year.&amp;quot;;  
                break;  
            case &amp;quot;June&amp;quot;:  
                message = &amp;quot;Middle of Year&amp;quot;;  
                break;  
            case &amp;quot;December&amp;quot;:  
                message = &amp;quot;End of Year&amp;quot;;  
                break;  
        }  
        @message;</body></html>
 

Looking to Hire ASP.Net Core Developer ?

Contact Now

 
Loops

Loops are used for executing same statement multiple times.
 

For Loops

If you know how many times you want to run the loop, you can use a For loop.
 

<html><body>
      @for(vari = 0; i&lt; 10; i++)  
       {<p>No @i</p>}</body></html>
 

For Each Loops

Foreach loop is used when you are working with array.

<html><body><ul><li> @foreach (var x in Request.variable) {</li><li>@x</li><li>}</li></ul></body></html>


 
Model

If you want to use model in HTML page then you have to declare model shown below.


@model modelname
Let&#39;s understand in the below example.
	@model data<h2>Detail:</h2>
<ul><li></li><li>Id: @Model.Id</li><li></li><li>Name: @Model.Name</li><li></li><li>Age: @Model.Age</li><li></li></ul>

Now, in the above example, we have used data model which is created in the Model folder by declaring @model data and by using @Model, we are using its properties.

Conclusion

Using Razor View Engine,you can make dynamic HTML Pages and use to get the data from model and database.

Razor-View-Engine-in-ASP-Net-Core

Table of Content

What is View Engine?

View Engine is responsible for producing an HTML response when called by the Controller Action method.The Controller Action methods can return different types of responseswhich are collectively called as Action Results. The View-result is the Action Result which produces the HTML Response.

The View Results are produced by the View Engine. When the Controller Action method call the view () or PartialView ()it calls the View Engine, which generate the HTML Response.View Engine is responsible for rendering the view into HTML form to the browser, View Engine is used to convert HTMLProgramming language to pure HTML.

Why is View Engine required in ASP.NET?

  1. View engine is responsible in creating HTML for View.
  2. It converts Pure HTML programming language to HTML
  3. View Engine is used to find the Related view for the action method.
  4. We are able to write C#/VB code on View
  5. View Engine is Needed to implement the strongly-typed View.

What is Razor View Engine?

The Razor View Engine is the default View Engine for the ASP.NET Core. It looks for Razor markup in the View File and parses it and produces the HTML response.

Razor Markup

The Controller in MVC invokes the View by passing the info to render. The Views must have the power to process the info and generate a response. This is done by the Razor markup, which allows us to use C# code in an HTML file. The Razor View Engine process these markups to get the HTML.

The files which contains Razor markup have a. cshtml file extension.The Razor syntax is shorter, simpler and straightforward to find out because it uses the C# or visual basic. The Visual Studio Intelligence support also helps with Razor syntax.

What is Razor Syntax in MVC?

There aretwo types of Razor syntax.

  1. Single statement block: It starts with @.
  2. Multi statement block: It is starts with @ {Coding part}

It needs to be always enclosed in @ {Coding part}

The semicolon “;” must be used to end the statements

Inline expressions (variables and functions) start with @

   

1) Single block Statements

A single statement block is used when a single line of code needs to write on View.

Example

To display current Date-time.

Create Index.cshtml View and add below code.


@{  
    ViewBag.Title = &amp;quot;Index&amp;quot;;  
}<div>  
   <label>Current-Date: @DateTime.Now.ToString()</label>
  
   <label>Current-Long Date: @DateTime.Now.ToLongDateString()
</label></div> 

Inspect browser and search for “DateTime.Now.ToString()” on browser.We cannot find the C# code on the browser as we did.

If you inspect browser and search for “DateTime.Now.ToString()” on the browser. it could not see the C# code on the browser. We can only see the only HTML code. This is the job of View Engine which convert C# code into pure Html format in the browser.

 

2) Multi statement block

We can also define a multiline statement block as a single-line statement block. In a multiline statement block we can define multiple code statements and doprocess. A multiline block is between opening and closing curly braces {}, but the opening brace will have the "@" character in the same line if we define the "@" and opening curly braces in different lines then it will give an error.

Example:

<div>
@{
	Var Java=100;
	Var Rubi=200;
	Var Ionic=400;
	Var sum=(Java+Rubi+Ionic);
  }
Sum of @Java and @Rubi and @Ionic is</div>

Now, let’s see how to write Razor code:

 
Variables

// Using the var keyword:  
var a = &amp;quot;Welcome to MyProgram&amp;quot;;  
var b = 300;  
var c = DateTime.Today;    
// Using data types:  
string greeting = &amp;quot;Welcome to MyProgram &amp;quot;;  
int counter = 300;  
DateTime day = DateTime.Today; 

Now, in the above example, we have declared variables using “var” keyword as well as throughits types forexample. string, int, Date Time. You can declare any of them. Italso uses int, float, decimal, bool and string data types.

 
Conditions

If statement

It starts with code block and its condition is written in parenthesis and the code which needs to be executed once condition gets true is written inside braces.

Let’s understand with the below example.


@{var a=60;}<html><body>
      @if (a&gt;50)  
       {<p>The a is greater than 50.</p>
       }</body></html>

In the above example, we have declared var “a” has a value of 60 and we are using if statement which is validating value of “a”. If the value is greater than 50 then the code written in side braces is executed.

 

If – Else statement

Initially the code block and its condition arewritten in parenthesis and code which is needs to be executed once the condition gets true is written inside braces and if it does not gettrue then code write inside else block gets executed.

Let’s understand with the below example.


@ {var a=60;}<html><body>
@if (a&gt;50)  
    {<p>The value is greater than 50.</p>
	}  
else  
	{
<p>The value is less than 50.</p>
	}</body></html>

In the above example, we have declared var “a” having a value of 60 and we are using if statement which is validating value of “a”. If the “a” is greater than 50 then the code written inside braces gets executed else the “a” is less than 50 and it will be executed.

 

Switch statement

It is used to check multiple times with different conditions.


@ {  
var month = DateTime.Now.Year.ToString();  
    var message = &amp;quot;&amp;quot;;  
}<html><body>
  @ {  
        switch (month)   
        {  
            case &amp;quot;January&amp;quot;:  
                message = &amp;quot;Starting of Year.&amp;quot;;  
                break;  
            case &amp;quot;June&amp;quot;:  
                message = &amp;quot;Middle of Year&amp;quot;;  
                break;  
            case &amp;quot;December&amp;quot;:  
                message = &amp;quot;End of Year&amp;quot;;  
                break;  
        }  
        @message;</body></html>
 

Looking to Hire ASP.Net Core Developer ?

Contact Now

 
Loops

Loops are used for executing same statement multiple times.
 

For Loops

If you know how many times you want to run the loop, you can use a For loop.
 

<html><body>
      @for(vari = 0; i&lt; 10; i++)  
       {<p>No @i</p>}</body></html>
 

For Each Loops

Foreach loop is used when you are working with array.

<html><body><ul><li> @foreach (var x in Request.variable) {</li><li>@x</li><li>}</li></ul></body></html>


 
Model

If you want to use model in HTML page then you have to declare model shown below.


@model modelname
Let&#39;s understand in the below example.
	@model data<h2>Detail:</h2>
<ul><li></li><li>Id: @Model.Id</li><li></li><li>Name: @Model.Name</li><li></li><li>Age: @Model.Age</li><li></li></ul>

Now, in the above example, we have used data model which is created in the Model folder by declaring @model data and by using @Model, we are using its properties.

Conclusion

Using Razor View Engine,you can make dynamic HTML Pages and use to get the data from model and database.