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:
<%foreach (var student in Students)
{ %> <% if (student.IsPassed)
{ %> <% 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. }
}
{ @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 isSystem.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). |
0 Comments