ASP.NET MVC Razor

Microsoft introduced the Razor View Engine in ASP.NET MVC 3 which is getting popular these days because of simplicity and easy to learn. This is the major change which has a built-in support in Visual Studio 2010 even there are other ASP.NET MVC view engines like NHaml and Spark used today.

We recently got MVCCONF virtual live conference in which the community took participate to understand the changes Microsoft made, the Razor was the one of them, because it is very simple, concise and takes minimal keystroke to accomplish the task in a page.

Why Razor is introduced by Microsoft Concise Code
Feel comfortable to type less character and get the same results as the default ASPX view engine.

Easy to Learn
No long path to learn the new language and concepts, if you already worked on ASP.NET MVC default view engine then and familiar with HTML code and a command on languages like C# or VB.NET, then what you need? You need an overview of new syntax which in turns has small learning curve.

Notepad is enough

No special text editor is required for working on Razor page, we can write the code using Notepad, but Visual Studio 2010 enhances the productivity and efficiency of work using great Intellisense.

Unit Test without Web Server
Not required a Web Server and even controller to test the view engine page. Razor provides the ability to unit test view which doesn’t need the special app domain and it can be hosted in any unit test project.

Pluggable as a Module
We can use more than one view engine page in one application. For instance we can add a view page of ASPX View Engine for simple pages and Razor for some complex pages which have some server side code, it is totally up-to you.

Add View

Add View

Difference between ASPX and Razor Syntax
The first difference  is the file extension which is changed in new Razor View Engine, .cshtml file extension is used for Razor. The second one is the Master page is replaced with _layout page for a consistent website look.

The third and most important difference is that we use <%=%> blocks to indicate the “code nuggest” within our HTML whereas in Razor we use @ character. The Razor parser is genius and underastand that the code block is started and implicitly closed that for us.

ASPX Syntax

<h1>Hello ASPX View Engine World</h1>

<p> Welcome to <%= yourname%> in the ASPX View Engine World<p>

<p> Today is <%= DateTime.Now.Month %>-<%= DateTime.Now.Day %>-<%= DateTime.Now.Year %></p>

<ul>

<% foreach(var language in languages)
{
        %> <li>Language:  <%= language.Name%></li><%
}

%>

</ul>

Razor Syntax

<h1>Hello Razor View Engine World</h1>

<p> Welcome to @yourname  in the ASPX View Engine World<p>

<p> Today is @DateTime.Now.Month-@DateTime.Now.Day-@DateTime.Now.Year </p>

<ul>

@foreach(var language in languages)
{
        <li>Language:  @language.Name</li>
}

</ul>

Resource:

http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

ASP.NET MVC , ,

2 comments


  1. thanks excellent information now we can easily embed c# code in html

  2. waqar

    Good article to start with Razor.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>