The Razor View Engine is an advanced view engine, available with MVC 3.0 and later versions. Razor uses the “@” character instead of “<% %>” as used by the ASPX View Engine. Razor does not require the code block to be closed, the Razor View Engine parsed itself and it is able to decide during run time that it is a presentation element (content) and that it is a code element. The Razor View Engine is compatible with a unit testing framework. The Razor template does not require the controller or webserver to host it, so views written in Razor are fully testable. The file extension of a Razor view is cshtml (for C#) and vbhtml (for VB.NET). By default all text from the @ expression is HTML encoded. Razor is not a new language. It is easy to learn. The main advantage of Razor, is that there is less transition between HTML and code because Razor provides an optimized syntax to generate HTML using a code focused templating approach.
Advantages of Razor View Engine
- Easy to Learn: Razor is easy to learn. We can also use our existing HTML skills.
- It is Compact, Expressive, and Fluid. Razor helps us to minimize the coding and provide us a fast and fluid coding work flow.
- The parser (available with Razor) is smart enough. It is also able to decide at run time what is a code element and what is a content element.For example, in the following code the @ character is also part of an email address, but Razor is smart enough to identify which is code and which is static content.
Please contact to firstname.lastname@example.org to more information Current Date time : @DateTime.Now
- Razor is not a new language but it is markup so that we can also use Razor with any language like C# and VB.
- Razor also supports the concept of layout pages (the same as Master Pages in ASPX View Engine), that allows us to define a common site template, in other words a common look and feel across all the pages within a web site/application.
- Razor does not require any special tool to write markup. We can also write our markup code with any old plain text editor like Notepad.
- The Razor View Engine is designed such that it also supports unit test views without requiring a controller and web server. This can be hosted in any unit project. There is no special application domain required.
- ASP.NET MVC has HTML helpers that are methods that can be invoked within a code block. All existing HTML extension methods can be used with a Razor View Engine without any code changes.
- The code looks clean.
- Powerful built-in validation of markup that helps us to avoid unwanted runtime exceptions due to errors in the view.
- The Razor View Engine has the section concept that is equivalent to content placeholders in the ASPX View Engine and that can be optional.
- The @model directive provides a cleaner and more concise way to define a strongly typed model.
One of the disadvantages of Razor is, it is not supported by visual editors like Dream Viewer.