ITORIAN    

  public string Welcome() {

      return "Abhimanyu's Thoughts";

  }

My First E-Book
Quick Report
Yearly Posts:
   i) Oct-Nov 2010: 12
   ii) In 2011: 380
   iii) In 2012: 32

You can find my articles and blogs on:
   i) itorian.com
   ii) c-sharpcorner.com
   iii) dotnetfunda.com
   iv) codeproject.com

Greatest Hits
People I Follow
Disclaimer
This is my personal website and the opinions I have expressed here is my own. For any accuracy I recommend to visit official websites like MSDN for Microsoft. I developed this website to share my technical skills.

A Quick tour of the Razor View Engine by comparing it to ASPX View Engine
[A quick post, to learn fundamental difference between ASPX and Razor View Engine introduced in MVC3.]
Abhimanyu Kumar Vatsa

IT Faculty || I blog on Microsoft Technologies || Mindcracker MVP || Founder of ITORIAN.COM
   

Introduction

In this post, I will give you a quick tour of the Razor View Engine syntax by comparing it to ASPX View Engine so that you can recognize the new elements when you enter in MVC3 learning.

Razor is the name of the new view engine introduced by Microsoft with the release of MVC3. The ASP.NET view engines processes web pages, looking for special elements that contain server-side instructions. As we already know the standard ASPX view engine relies on the <% and %> elements, which is familiar to all ASP.NET developers. But with Razor View Engine, the MVC development team has introduced a new set of syntax elements, centered on the @ symbol.

If you are familiar with the <% %> syntax, you won’t have too many problems with Razor, although there are a few new rules.

Razor views have a file extension of .cshtml, as opposed to the .aspx extension used in previous MVC releases and in ASP.NET Web Forms, we can still use the ASPX view engine in an MVC3 project but try to learn Razor engine, because it seems to be a strong area of focus.

Now, let’s look at the ASPX Engine page and Razor Engine page:

ASPX View Engine Page

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

 

<!DOCTYPE html>

 

<html>

<head runat="server">

    <title>Index</title>

</head>

<body>

    <div>

        <ul>

            <% for (int i = 1; i <= 10; i++) { %>

                <li><% =i%></li>

            <% } %>

        </ul>

    </div>

</body>

</html>

 

In above page, I am writing 1 table by switching to C# and HTML mode again and again. This might be simple by using Razor, look at the example below.

Razor View Engine Page

@{

    ViewBag.Title = "Index";

}

 

<div>

    <ul>

        @for (int i = 1; i <= 10; i++)

        {

            <li>@i</li>

        }

    </ul>

</div>

 

In above example, I’m writing the same page by using Razor View Engine and it is bit easy than ASPX Engine. Here we don’t need to switch over HTML and C# again and again.

That’s why Razor View Engine is much simpler than ASPX View Engine. I hope you like it. Thanks.
 
Happy Coding !!



Join us on Facebook. Join us on Twitter.

   



Approved Comments

Connie 1/20/2012 8:55:39 AM
Connie: I thank you humbly for sharing your wisdom
Author: :)
Akiii
Akiii: Hi Abhi.... nice article, looking forward for more differences between these two engines..! Thanks
Author: surely, posts under queue..
Suvendu Shekhar Giri
Suvendu Shekhar Giri: nice article to learn the basic difference. Thanks for sharing
Author: oh! its you. Thanks for the comment sir.