Introduction
View Engine is used to render the HTML to the browser. The view engine templates have different syntax to implementation. By default ASP.net MVC support ASPX and Razor view engine. There is many more third party view engine like spark, Nhaml etc. are also available for MVC. We can also write our own view engine.
ASPX View Engine
The syntax used for writing view with ASPX view engine is a same as syntax that is used in ASP.net web forms. File extensions are also same as ASP.NET web forms (like .aspx, .ascx, .master). This view engine is default view engine for MVC 1.0 and MVC 2.0. Implement unit testing framework with ASPX view engine is very difficult. ASPX is used <%= %> or <%: %> to render server side content. We can choose any language with CodeDom provider. There is on demand or precompiled view is supported by ASPX view engine. ASPX view engine is also known as Web Form view engine
Loop and condition example with ASPX view engine
The syntax used for writing view with ASPX view engine is a same as syntax that is used in ASP.net web forms. File extensions are also same as ASP.NET web forms (like .aspx, .ascx, .master). This view engine is default view engine for MVC 1.0 and MVC 2.0. Implement unit testing framework with ASPX view engine is very difficult. ASPX is used <%= %> or <%: %> to render server side content. We can choose any language with CodeDom provider. There is on demand or precompiled view is supported by ASPX view engine. ASPX view engine is also known as Web Form view engine
Loop and condition example with ASPX view engine
<ul>
<%foreach (var item in Products)
{ %>
<% if (item.IsInStock)
{ %>
<p><%=item.ProductName%> is in stock</p>
<% }
else
{ %>
<p><%=item.ProductName%> is not in stock</p>
<% } %>
<%} %>
</ul>
<%foreach (var item in Products)
{ %>
<% if (item.IsInStock)
{ %>
<p><%=item.ProductName%> is in stock</p>
<% }
else
{ %>
<p><%=item.ProductName%> is not in stock</p>
<% } %>
<%} %>
</ul>
Razor View Engine
The Razor view engine is an advanced view engine, available with MVC 3.0 and later version. Razor is use @ character instead of <% %> used by ASPX view engine. Razor does not required close the code block, Razor view engine parsed itself and it able to decide run time which is presentation element (content) and which is a code element. Razor view engine is compatible with unit testing framework. Razor template does not required controller or webserver to host, so views written in Razor is fully testable. File extension of Razor view is cshtml (for C#) and vbhtml (for VB.NET). By default all text from @ expression is HTML encoded. Razor is not a new language. It is easy to learn. The main advantage of Razor, there is less transition between HTML and code because Razor is provide an optimized syntax to generate HTML using Code focused templating approach.
Loop and condition example with Razor view engine<ul>
@foreach (var item in Products)
{
@if(item.IsinStock)
{
@item.ProductName is in stock
} else {
@item.ProductName is in stock
}
}
</ul>
One of the disadvantages of Razor is, it is not supported by visual editor like dream viewer.
The Razor view engine is an advanced view engine, available with MVC 3.0 and later version. Razor is use @ character instead of <% %> used by ASPX view engine. Razor does not required close the code block, Razor view engine parsed itself and it able to decide run time which is presentation element (content) and which is a code element. Razor view engine is compatible with unit testing framework. Razor template does not required controller or webserver to host, so views written in Razor is fully testable. File extension of Razor view is cshtml (for C#) and vbhtml (for VB.NET). By default all text from @ expression is HTML encoded. Razor is not a new language. It is easy to learn. The main advantage of Razor, there is less transition between HTML and code because Razor is provide an optimized syntax to generate HTML using Code focused templating approach.
Loop and condition example with Razor view engine<ul>
@foreach (var item in Products)
{
@if(item.IsinStock)
{
@item.ProductName is in stock
} else {
@item.ProductName is in stock
}
}
</ul>
One of the disadvantages of Razor is, it is not supported by visual editor like dream viewer.
Advantages of Razor view Engine
- Easy to Learn: Razor is easy to learn. We can also use our existing HTML skills.
- It is Compact, Expressive, and Fluid. Razor help us to minimizes the coding and provide us fast and fluid coding work flow.
- The parser (available with Razor) is smart enough. It is also able to decide run time which is code element and which is a content element.
For example in below code @ character is also part of an email address, but Razor is smart enough to identify which is code and which is static content.
<p>
please contact to abc@gmail.com to more information
Current Date time : @DateTime.Now
</p>
- Razor is not new language but it is markup so that we can also use Razor with any language like C#, VB.
- Razor is also support the concept of layout pages (same as Master page in ASPX view engine), which is allow us to define common site template i.e. common look and feel across all the pages within web site/application.
- Razor does not require any special tool to write markup. We can also write our markup code with any old plain text editor like notepad.
- The Razor view engine is design such way that it also supports unit test views without requiring controller and web server. This can be hosted in any unit project. There is no special application domain required.
- ASP.NET MVC has concept of HTML helper which are the methods that can be invoked with in code block. All existing HTML extension methods can be used with Razor view engine without any code changes.
- Code looks clean.
- Powerful Built in validation of markup, which help us to avoid unwanted runtime exception due to error in view.
- Razor view engine has Section concept which is equivalent to content placeholders in ASPX view engine and which can be optional.
- @model directive provides cleaner and more concise way to define strongly typed model.
Example:
@model List<MyMVCapplication.EmployeeMaster>
or
@model MyMVCapplication.EmployeeDetails
Razor View Engine VS ASPX View Engine
Razor View Engine | ASPX View Engine (Web form view engine) |
Name space used by Razor View engine is System.Web.Razor | Name space used by ASPX View engine is System.Web.Mvc.WebFormViewEngine |
The file extensions used by Razor view engine are different from web form view engine. It uses cshtml with c# and vbhtml with vb for views, partial view, templates and layout pages. | The file extensions used by Web Form view Engine are like ASP.net web forms. It uses aspx extension for view, aspc extension for partial view or user control or templates and master extension for layout/master pages. |
Razor view engine is advanced view engine that was introduced with MVC 3.0. This is not new language but it is markup. | Web form view engine is default view engine and available from beginning of MVC |
Razor has syntax, that is very compact and help us to reduce typing. | Web form view engine has syntax that is same as asp.net forms application. |
Razor View engine is used @ to render server side content. | ASPX/web form view engine is used <%= %> or <%: %> to render server side content. |
By default all text from @ expression is HTML encoded. | There is different syntax (<%: %>) to make text HTML encoded. |
Razor does not required close the code block, Razor view engine parsed itself and it able to decide run time which is content element and which is a code element. | Web form view engine required proper close of code block otherwise it threw run time exception. |
Razor View engine prevents Cross Site Scripting (XSS) attack by encodes the script or html tags before rendering to view. | Web form View engine does not prevent Cross Site Scripting (XSS) attack. |
Razor engine support Test Driven Development (TDD). | Web Form view engine does not support Test Driven Development (TDD) because it depend on System.Web.UI.Page class which makes the testing complex. |
Razor is used @* … *@ for multiline comment. | ASPX view engine used <!--...--> for markup and /* … */ for C# code. |
There is only three transition character with Razor view engine. | There is only three transition character with Razor view engine. |
Razor view engine is bit slower than ASPX view engine. |
Conclusion
Razor provide new view engine with streamlined for code focused templating. Razor's syntax is very compact and improves readability of mark up and code. By default MVC support ASPX (web Forms) and Razor View engine. MVC is also support third party view engine like spark, Nhaml, NDjango, SharpDOM etc. Now ASP.NET MVC is open source.
Razor provide new view engine with streamlined for code focused templating. Razor's syntax is very compact and improves readability of mark up and code. By default MVC support ASPX (web Forms) and Razor View engine. MVC is also support third party view engine like spark, Nhaml, NDjango, SharpDOM etc. Now ASP.NET MVC is open source.
No comments:
Post a Comment