Microsoft has introduced a new view engine called “Razor” for ASP.NET MVC as an alternative to the WebForms view engine (ASPX).
The Razor view syntax is based on the C# programming language. Instead of applying the ASP.NET .ASPX markup syntax usingblocks to indicate code blocks, the Razor view syntax starts of a code block using a @ character and does not require explicit closing of the code-block.
Other prominent benefits of the Razor View Syntax:
- Razor view syntax is clean and concise, requiring a minimum number of characters and keystrokes.
- The ease of learning Razor View Syntax makes it a welcome introduction, as it can be learned in parts since it is based on existing languages like C# and Visual Basic.
- The Microsoft Visual Studio 2010 includes IntelliSense and code colorization for Razor view syntax.
- Razor view syntax can be unit tested without requiring to run the application or launch a web server.
- Razor also supports the Master page concept using “layout pages” – which allows you to define a common site template, and then inherit its look and feel across all the views/pages on your site.
- Developers can add breakpoints to views in order to debug Razor views. This can be very helpful for inspecting the values of view model properties. For example, add a breakpoint to a view just as was done in the code file: right click the Razor statement that you are interested in and select Breakpoint -> Insert Breakpoint.
How to add the view engine into ASP.NET project?
The Add View dialog box in ASP.NET MVC 3 or 4 lets you choose the view engine you want to work with, and the New Project dialog box lets you specify the default view engine for a project. You can choose the Web Form’s view engine (ASPX), Razor, or other open-source view engines.
If one chooses to use the Razor view engine, the file extension will be .cshtml or .vbhtml instead of .aspx or .ascx.