Razor Engine in MVC Framework

MVC Framework by default provides Razor Engine in MVC and ASP .Net MVC support Web Form Engine. Razor View Engine is responsible for rendering the view into HTML form to the browser response in the MVC application.

In this article, I am going to discuss the ideas about Razor Engine in MVC. Please read our previous articles before moving on to this article where we discussed the Action Selector in ASP.NET MVC. As part of this article, we are going to discuss the following contents which are related to Razor Engine in MVC.

Razor Engine in MVC

MVC Framework by default provides Razor View Engine and ASP .Net MVC support Web Form Engine.
Razor View Engine is responsible for rendering the view into HTML form to the browser response in MVC application.
Now ASP .Net MVC is open source so you can use some other third-party engine in your MVC application such as Spark.

Let us discuss some advantages of Razor View Engine.

Advantages of Razor Engine in MVC

  • Razor Engine introduced on MVC 3.0 framework.
  • System.Web.Razor is namespace for Razor Engine.
  • File extensions for view are “.cshtml” for C# and “.vbhtml” for VB.
  • Razor syntax are advance syntax that are compact, expressive and reduces typing.
  • It prevents XSS attacks (Cross-Site Scripting Attacks) because it encodes the script or html tags.
  • Razor syntax are easy to learn.
  • Auto Intellisense supported for razor syntax in Visual Studio.

Disadvantages of Razor Engine in MVC

  • It is bit slow as compare to webform engine.
  • It does not support design mode so you can’t able to see page view in Visual Studio.

Now time to learn some razor syntax for the development.

One Line Statement

  • This type of statement have only one line of code.
  • Each statement always start with @ symbol.
  • It does not require a semicolon at the end of the expression.
Date and Time : @DateTime.Now.ToString()

Multi Line Statement

  • This case write group of statement in an one block by using @{} code block.
  • Each statement must have ending with semicolon.
@{
	string name = "InfoSyntax";
	string NowDT = DateTime.Now.ToString();
}

Welcome to @name
Date and Time : @NowDT

Variable Declaration

  • Must be use the code block for variable declaration.
@{
	string name = "InfoSyntax";
}

Welcome to @name

Text Data inside code block in Razor

  • @: or syntax is used for declaring the text data in razor code block.
@{
	string name = "TutorialsLife";
	@:Welcome to @name

	Welcome to @name
}

Razor Engine has so many syntaxes for developing the application. All these syntaxes you will use in an upcoming session.

Warm UP – Razor Engine in MVC Framework

  • MVC Framework by default provides Razor View Engine and ASP .Net MVC support Web Form Engine.
  • System.Web.Razor is the namespace for Razor Engine.
  • File extensions for view are “.cshtml” for C# and “.vbhtml” for VB.
  • It is a bit slow as compare to the webform engine.
  • Each statement always starts with @ symbol.
  • This case writes a group of statements in one block by using @{} code block and each statement must have ended with a semicolon.
  • Must use the code block for variable declaration.
  • @: or syntax is used for declaring the text data in razor code block.

Please read more about Razor Engine in MVC Framework.

Leave a Comment

Your email address will not be published. Required fields are marked *