Getting Started with Adobe After Effects - Part 6: Motion Blur

Upload Image Close it
Select File

This tutorial will help you to get started with ASP.NET MVC

Getting started with ASP.NET MVC

Getting Started with ASP.NET MVC - Part 4: Introduction to Different view engines Razor and ASP.NET

May 8 2012 12:00AM by Jalpesh   

In previous chapter, we have learned about controller, views, action results etc. In this chapter we will learn about the different type of view engines.

What is view engine?

As we have learned in previous chapters, in ASP.NET MVC, view is used to render response given by the server. Once all server side processing is completed, it will render the view in terms of HTML to browser. View engine is responsible for creating HTML from the view. A view is mostly some kind of mix-up of HTML and programming language. View engine interprets the programming language part and render the response as HTML.

Different type of view engines:

There are two type of view engines available by default with ASP.NET MVC3.

  1. ASP.NET Basic View Engine
  2. Razor View Engine that comes with ASP.NET MVC3 only.

We can select this view engine at the time of creation of ASP.NET MVC project. Let’s take a example where we are going to compare both view engines. So let’s create a simple ASP.NET MVC3 application.

To create a new application goto File->New project in Visual Studio. It will open up new project dialog box like following:

New Project

Once you click OK, it will ask for application type where you have option to choose your view engine like following:

Type of Applications

As you can see in above screenshot, you can see that there are options to choose view engine. Visual studio 2010 supports by default two view engine ASPX (ASP.NET default view engine) and another one is Razor view engine.

I have chosen aspx, which is basic view engine for mvc application. Therefore, once you complete your creation application, it will create mvc application with aspx view engine. ASPX view engine comes <%:%> syntax. Therefore, if you see index view it will have following syntax.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewBag.Message %></h2>
        To learn more about ASP.NET MVC visit <a href="" title="ASP.NET MVC Website"></a>.

The same view with razor view engine will look like following:

    ViewBag.Title = "Home Page";

    To learn more about ASP.NET MVC visit <a href="" title="ASP.NET MVC Website"></a>.

You can see that only difference between two is the syntax. The aspx view engine supports <%: %> syntax while razor view engine supports @ syntax. Another difference is the extension of views. ASPX view engines will support .aspx extension while Razor view engine will support .cshtml extension. You can see that in the following screenshots.

Following is an aspx view engine screenshot:

ASPX View Engine

While below is screenshot for razor syntax:

Razor View Engine

Now let us take simple for-loop example for both to see what are the differences in syntax. Below is syntax for aspx view engine.

<% foreach(var item in Model) { %>
        <td><%: item.Name %></td>
<% } %>

Following is the syntax for razor view engine:

@foreach(var item in Model) {

Therefore, the only difference is syntax. Razor syntax is clean syntax while aspx syntax is little bit messy as compared to the razor syntax. However, rendering time will be less for aspx syntax and while it will be higher for razor syntax. So based on type of application, you need to choose the view engine. If your application performance is critical and you want to save each and every second of it, then you should go for aspx view engine but If performance is not your criteria and you want a clear and much simpler syntax then you should go for razor syntax.

Third party view engines:

Until now, we have seen in built view engines with ASP.NET MVC but there are some third party view engines are also available which we can use in MVC. Followings are some of them:

  1. Spark View Engine:

    You can fine more information about spark engine from following site.

    Once again, here syntax of view engine will be different then above two view engines. It will use ${} syntax to render HTML. Simple example of that is following.

    beta is my favorite.

    ${name} is okay too I suppose.

    You can find more information about Spark view engine syntax from following link where it contains documentation.

  2. NHAML: This view engine pronounced as enamel is pure .NET implementation of Ruby rails popular HAML view engine.

    You can find lots of information for this view engine from the following link where you will get all the information how to configure that etc.

    As we know, here also syntax will be slightly different then above view engines. It will use # and % to render HTML. Following is an example of that:

    #collection .item .description What a cool item!

You can find more information about syntax from following link.

  1. Nvelocity:

    Nvelocity is .NET port of the popular Java project velocity. You can find more information about this view engine from the following site.

There are many more engines available where every view engine is having their own pros and cons. You need select view engine as per your requirement. There is a very good comparison of all the view engines available for mvc at the following link. You will find lots of view engine information from there.

That is it. Hope you like it. In next chapter, we will learn Razor syntax in detail.

15 · 11% · 3548


Your Comment

Sign Up or Login to post a comment.

"Getting Started with ASP.NET MVC - Part 4: Introduction to Different view engines Razor and ASP.NET" rated 5 out of 5 by 6 readers
Getting Started with ASP.NET MVC - Part 4: Introduction to Different view engines Razor and ASP.NET , 5.0 out of 5 based on 6 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]