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
- A 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.
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:
{ %>
<% if (student.IsPassed)
<%=student.Name%> not promoted to next Grade.
<% } %>
<% } %>
Code Snippet for Razor View Engine serving same purpose:
{
@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.
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:
|
As it’s new and advanced View Engine, it’s extensions are totally different.
|
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). |
- 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:
- Browser back button JavaScript
- Webforms Vs MVC
- ASP.NET MVC3 Vs MVC4 Vs MVC5
- ASP.NET Web API by Example
- jQuery REST call
- WCF Vs Web Services
- Essential WCF Tutorial
- WCF Hosting (Console | Windows Service | IIS)
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions