Razor View Engine in ASP .NET MVC 3

16 Jan

Simple overview

The Razor View Engine is new to ASP.NET MVC 3 and is the default view engine moving forward. This post focuses on Razor and does not cover the Web Forms View Engine. Razor is the response to one of the most requested suggestions received by the ASP.NET MVC feature team — to provide a clean, lightweight simple view engine that didn’t contain the “syntactic cruft” contained in the existing Web Forms View Engine. Many developers felt that all that syntactic noise required to write a view created friction when trying to read that view. This request was finally answered in version 3 of ASP.NET MVC with the introduction of the new Razor View Engine.

Razor provides a streamlined syntax for expressing views that minimizes the amount of syntax and extra characters. It effectively gets out of your way and puts as little syntax as possible between you and your view markup. Many developers who have written Razor views have commented on feeling the view code just flowing from their fingertips, akin to a mind-meld with their keyboard. This feeling is enhanced with the first-rate IntelliSense support for Razor in Visual Studio 2010.

Razor accomplishes this by understanding the structure of markup so that it can make the transitions between code and markup as smooth as possible. To understand what is meant by this, some examples will help. The following example demonstrates a simple Razor view that contains a bit of view logic:

// this is a block of code. For demonstration purposes, we’ll
// we’ll create a “model” inline.
var items = new string[] {“one”, “two”, “three”};
<head><title>Sample View</title></head>
<h1>Listing @items.Length items.</h1>
@foreach(var item in items) {
<li>The item name is @item.</li>

The previous code sample uses C# syntax which means the file has the .cshtml file extension. Similarly, Razor views which use the Visual Basic syntax will have the .vbhtml file extension. These file extensions are important, as they signal the code language syntax to the Razor parser.

Understanding Razor Syntax:

Again: Razor is the name of the new view engine in MVC 3. The ASP.NET view engine processes web pages, looking for special elements that contain server-side instructions. As we’ve noted earlier, the standard ASPX view engine relies on the <% and %> elements, which are familiar to all ASP.NET developers. With Razor, the MVC development team has introduced a new set of syntax elements, centered on the @ symbol. By and large, if you are familiar with the <% %> syntax, you won’t have too many problems with Razor, although there are a few new rules. In this section, we’ll give you a quick tour of the Razor syntax so you can recognize the new elements when you see them. We aren’t going to supply an exhaustive Razor reference; think of this more as a crash course in the syntax. We’ll explore Razor in-depth as we continue through the book.

Understanding Razor View Rendering:

The Razor View Engine compiles the views in your applications to improve performance. The views are translated into C# classes, and then compiled, which is why you are able to include C# code fragments so easily. It is instructive to look at the source code that Razor views generate, because it helps to put many of the Razor features in context. Next example shows a simple Razor view that takes an array of strings as the view model object.

@model string[]
ViewBag.Title = “Index”;
This is a list of fruit names:
@foreach (string name in Model) {

The views in an MVC application are not compiled until the application is started, so to see the classes that are created by Razor, you need to start the application and navigate to an action method. Any action method will do, since the initial request to an MVC application triggers the view compilation process.

Conveniently, the generated classes are written to the disk as C# code files and then compiled, which means that you can see the C# statements that represent a view. You can find the generated files in c:\Users\yourLoginName\AppData\Local\Temp\Temporary ASP.NET Files on Windows 7, or the equivalent folder on your operating system version and installation location.

Finding the code file generated for a particular view requires a bit of poking around. There are usually a number of folders with cryptic names, and the names of the .cs files don’t correspond to the names of the classes they contain.

Leave a comment

Posted by on January 16, 2012 in ASP .NET


Tags: , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: