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. Thanksv