ASPX View Engine Vs Razor View Engine in ASP.NET MVC

By | October 21, 2014

In this post we are going to compare two major View Engines in ASP.NET MVC including ASPX View Engine and Razor View Engine. Let’s first understand What a View Engine is?

What is a View Engine in ASP.NET MVC?

“View Engine in ASP.NET MVC is used to translate our views to HTML and then render to response.”

Multiple View Engines are available for MVC including ASPX, Razor, NHaml etc. Normally in ASP.NET MVC, a View Engine translates view into HTML by:

  • Providing implementation of IViewEngine (as template provider)
  • IView (as rendering template) and
  • Template Engine for parsing and compiling view file into executable code.

We can even use multiple View Engines in parallel (if needed). For more details about ViewEngine class, please follows here.

ASPX Vs Razor

Understanding Syntax Difference

Being software developer, we are normally concerned about code syntax differences when using either of these two View Engines. So, in order to get better understanding, please look into following code snippet written using both ASPX and Razor View Engine in order.

ASPX View Engine is also known as Web Form View Engine and inherits it’s syntax as “<%= %>” or “<%: %>” for rendering server-side contents:

<%foreach (var student in Students)
{ %>
                 <% if (student.IsPassed)
                 { %>
                          <%=student.Name%> promoted to next Grade.
               <% }else{ %>
                          <%=student.Name%> not promoted to next Grade.
               <% } %>
<% } %>

Code Snippet for Razor View Engine serving same purpose:

@foreach (var student in Students)
{
            @if(student.IsPassed)
             {
                   @student.Name promoted to next Grade.
             } else {
                  @student.Name not promoted to next Grade.
             }
}

It’s quite clear that Razor syntax is clean and simpler as compared to ASPX syntax.ASP.NET MVC Online Test

Detailed Difference Between ASPX View Engine and Razor View Engine

Now, following table describe the difference in both View Engines in more details.

ASPX View Engine

Razor View Engine

System.Web.Mvc.WebFormViewEngine is the namespace for ASPX View Engine. Namespace for ASPX view Engine is System.Web.Razor.
File Extension for this View Engine is similar to WebForm as:

  • .aspx, for Views just like Web Form pages.
  • .ascx, for Partial Views & Editor Template just like User Controls.
  • .master, for Layout and Master Pages just like Master Pages in Web Forms.
As it’s new and advanced View Engine, it’s extensions are totally different.

  • .cshtml (Razor C#), For all including Views, Partial Views, Editor Template and Layout Pages.
  • .vbhtml (Razor VB.NET), For all including Views, Partial Views, Editor Template and Layout Pages.
From the beginning, ASPX View Engine was part of ASP.NET MVC. Razor View Engine was introduced in ASP.NET MVC v3.
ASPX View Engine uses syntax same as that of Web Form pages (already demonstrated above). Razor Syntax is different as compared to Web Forms. Using Razor syntax, developer type comparatively less code which is is easy to understand.
ASPX syntax is inherited from Web Forms, so it’s understandable for Web Forms developer but it’s not that much clean as compared to Razor View Engine. As Razor View Engine is introduced later in MVC3, it’s syntax is designed to be clean, expressive and easy to learn.
ASPX View Engine does nothing to avoid Cross-Site Scripting attacks by default. By default, Razor View Engine encodes html tags or scripts before it’s being rendered to view that avoids Cross-Site Scripting attacks.
ASPX View Engine is comparatively fast. Razor View Engine is slow as compared to ASPX View Engine.
It supports design view in Visual Studio. It doesn’t support design view in Visual Studio.
No support for TDD (Test Driven Development). Supports TDD (Test Driven Development).
Question 1. View Engine that Support TDD (Test Driven Development) [Select One]?

  • A. ASPX View Engine
  • B. Razor View Engine

Question 2File Extension used for Razor View Engine are [Choose all that apply]?

  • A. .cshtml
  • B. .master
  • C. .aspx
  • D. .vbhtml

To further test your ASP.NET MVC skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.

Hopefully, reader will have better understanding about both ASPX and Razor View Engines. In order to start working with different View Engines, let’s implement your first ASP.NET MVC application.

Other ASP.NET MVC and Related Articles:

Top 10 Interview Questions and Answers Series: