SlideShare a Scribd company logo
Introduction of View Engine
There are two types of view engine supported by ASP.NET MVC. The Razor and ASPX view
engine View Engines are responsible for rendering the HTML from your views to the
browser. The view engine template will have different syntax for implementation.
What is @Razor ?
Razor using an @ character instead of aspx's <% %> and Razor does not require you to
explicitly close the code-block, this view engine is parsed intelligently by the run-time to
determine what is a presentation element and what is a code element. Razor view engine is
compatible with unit testing frameworks. This is the default view engine in MVC 3 and
MVC 4. Web pages with Razor syntax have the special file extension cshtml (Razor
with C#) or vbhtml (Razor with VB).
What is <%ASPX%> ?
This is the default view engine for MVC 1 and MVC 2. The syntax for writing views with this
engine is the same syntax that the ASP.NET Web Forms uses and the file extensions are also
taken from ASP.NET Web Form (.aspx, .ascx, .master) .
Syntax and Uses of @Razor and <%ASPX%>
It will be easy for ASP coder to learn about ASPX View Engine, and if you are comfortable
with C# language then you can easily go with Razor view engine. Let's go through how we
can use Razor and or ASPX view engine in our application. Let's start reviewing some of the
usages. In this article I am try to cover both syntax with each other by grouping, this help us
to understand and compare the syntax also it will help to you to remember easily.
Comment in ASPX and Razor View Engine
Comments are delimited in Razor syntax by @* *@ where in ASPX comments are delimited
by <%-- --%>
?
1
2
@*Your Razor Comment Here*@
<%--Your Razor Comment Here--%>
In a C# code block the comment for Razor or ASPX will be // and /* */ comment delimiters.
Following code shows comments inside a c# code block.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@{
//Razor
//Single line comment
/* Multi
line comment
*/
}
<%
//ASPX
//Single line comment
/* Multi
line comment
*/
%>
Code Block in ASPX and Razor View Engine
Writing a server side code blocks in the source html is shown below for Razor
?
1
2
3
4
5
@{
string strVariable = "Razor Stirng";
bool isRazor = true;
string[] arrRazor = new string[] { "MVC", "RAZOR", "AXPS"};
}
The above code declares a string variable, bool variable and a array variable in Razor and
the variables are got initialized with appropriate data in it. This code block is written in
.cshtml file for Razor with C# and .vbhtml file for Razor with VB (must use VB syntax). The
same code is written in .aspx (ASPX C#).
?
1
2
3
4
5
<%
string strVariable = "Razor Stirng";
bool isRazor = true;
string[] arrRazor = new string[] { "MVC", "RAZOR", "AXPS"};
%>
Razor and ASPX with HTML Markup
Following code block shows how we can integrate the Razor Code block with html markup
when working in a dynamic web page we often required mixing code and html tags.
?
1 <span>@Your Code</span>
The above code is written for Razor(C#) and the below code will acheive the same in
ASPX(C#) view engine.
?
1 <span><%:YourCode %></span>
There may be some other cases , in which we can marge HTML tag with Razor or ASPX view
engine.
?
1
2
3
4
5
6
7
8
@if (isRazor)
{
<span>ViewEngine:Razor</span>
}
else
{
<span>ViewEngine:Other</span>
}
The above code used with ASPX view engine will be.
?
1
2
3
4
5
6
7
8
<%if (isRazor)
{
<span>ViewEngine:Razor</span>
}
else
{
<span>ViewEngine:Other</span>
} %>
You can do more mixing of HTML tags with Razor and ASPX View Engine, Following code
block shows how you can do that.
?
1
2
Your Sample Html Code or Text @RazorCode (@AnotherRazorCode)
Your Sample Html Code or Text <%: ASPXCode %> (<%:AnotherASPXCode %>)
When the above code will rendered with the value of RazorCode and AnotherRazorCode
variable is replaced as we do in server side coding. Similarly you can assign dynamic HTML
attribute for any HTML tags. Let’s understand by an example.
?
1
2
<input type="checkbox" checked="@isRazor">
<input type="checkbox" checked="<%:isASPX%>">
Conditional Statement in ASPX and Razor View Engine
Below is a sample Conditional statement for Razor (C#).
?
1
2
3
4
5
6
@if(isRazor)
{
<p>View Engine:Razor</p>
} else {
<p> View Engine:Other</p>
}
Below is a sample Conditional statement for ASPX (C#).
?
1
2
3
4
5
6
7
8
<% if (isASPX)
{ %>
<p> View Engine:ASPX </p>
<% }
else
{ %>
<p> View Engine:Other </p>
<% } %>
The above usage gives us a very easy approach for rendering things conditionally.
Looping Statement in ASPX and Razor View Engine
The below statement in Razor view engine is used for rendering the ListItem .
?
1
2
3
4
5
6
<ul>
@foreach (var item in ListItem)
{
<li>@item</li>
}
</ul>
Razor view engine is smart enough to identify the tag as an <li> html tag and the @item as
a server side value. The <li> will be rendered multiple times with the list of products.
?
1
2
3
4
5
6
<ul>
<%foreach (var item in ListItem)
{ %>
<li><%=item%></li>
<%} %>
</ul>
Calling Server Side in Method in ASPX and Razor View Engine
To understand to call let’s assume that we have a static method named ViewEngineMethod
in a class and called inside a tag using the appropriate view engines syntax.
?
1 <p>@{RazorASPXViewEngine.Models.SampleUtility.ViewEngineMethod();}</p>
This code will be rendering with the output returned from the ViewEngineMethod()
?
1 <p><%=RazorASPXViewEngine.Models.SampleUtility.ViewEngineMethod();%></p>
Creating a Delegate in Method in ASPX and Razor View Engine
The delegates Razor can be define as templates build by C# and HTML. Here we have used
Func instead of using Func. and we have used the 'item' to get the value inside the Func<>.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@{
Func<string[], object=""> SomeList =
@<ul>
@foreach (string p in item)
{
<li>@p</li>
}
</ul>;
}
Some listing from delegate-
@SomeList(ListItem);
</string[],>
Creating a method using ASPX and Razor View Engine
The @functions block in Razor helps you to define utility functions directly in the view.
?
1
2
3
4
5
6
7
@functions{
public string myMethod()
{
return "My Method";
}
}
Name:@myMethod();
We should create the function only when the function is specific to the view and can't be
done using other conditional statement. By using @function we can separate view specific
logic from controller.
Escape Character in ASPX and Razor View Engine
To escape an @ symbol just add @@ and it will render a single @ symbol. Razor language
parser is very smart to konw the @ symbol is used for a static content or dynamic content.
Let's take an example to understand this.
?
1
2
3
@@@ViewEngine
@@ViewEngine
example@DotNet-Tutorial.com
The fist line will print @C# where as the second line will print @@ViewEngine. The third one
will be parsed intelligently by the parser and will print the email address example@DotNet-
Tutorial.com. The second line printed @@ViewEngine because of the escape symbol the
variable will not be considered/evaluated.
Summary: Razor view engine is widely used because of the lightweight syntax, readability,
maintanability, TDD, and more. Razor uses @ symbol whereas ASPX uses <% %>. File
extensions are different for Razor (C#-cshtml, VB-vbhtml) and ASPX(aspx, ascx, master) .
Namespace for Razor is System.Web.Razor and for ASPX it's
System.Web.Mvc.WebFromViewEngine. Razor sytax is very clean and easy to learn than
ASPX view engine.

More Related Content

ODP
Play Template Engine Based On Scala
PDF
DOCX
Spring annotations notes
PPS
Commenting Best Practices
PPT
Red5 - PHUG Workshops
PPT
Controls
PDF
Introductontoxaml
PDF
Murach: How to validate data in asp.net core mvc
Play Template Engine Based On Scala
Spring annotations notes
Commenting Best Practices
Red5 - PHUG Workshops
Controls
Introductontoxaml
Murach: How to validate data in asp.net core mvc

What's hot (13)

PPTX
Java script basics
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PPTX
Spring Surf 101
PPT
Apache Camel - WJax 2008
PPT
Jsp And Jdbc
PPTX
Knockoutjs – components
DOCX
Java script basics
PPTX
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
PPT
C#/.NET Little Pitfalls
PPT
Hibernate training
ODP
Os Leonard
PPTX
Module 2: C# 3.0 Language Enhancements (Slides)
PPT
JSP diana y yo
Java script basics
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Spring Surf 101
Apache Camel - WJax 2008
Jsp And Jdbc
Knockoutjs – components
Java script basics
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
C#/.NET Little Pitfalls
Hibernate training
Os Leonard
Module 2: C# 3.0 Language Enhancements (Slides)
JSP diana y yo
Ad

Similar to Asp 4-razor-introduction of view engine (20)

PPS
Razor new view engine asp.net
PPTX
Razor and the Art of Templating
PPTX
ASP.NET MVC One Step Deeper
PPTX
Asp.net With mvc handson
PPT
Super billing asp.net
PPTX
PPT
ASP.NET MVC introduction
PPTX
Overview of MVC Framework - by software outsourcing company india
PPT
Asp.net mvc
PDF
java-scriptcdvcx vnbm,azsdfghjkml;sxdfcgmndxfcgvhb nmfctgvbhjnm ,cfgvb nm,xc ...
PPTX
CSC PPT 12.pptx
ODP
SCDJWS 5. JAX-WS
PDF
Jsplecture
PPTX
Java ppt
PDF
Create an application with ember
PPTX
Simple mvc4 prepared by gigin krishnan
PPTX
Salesforce Lightning Web Components Overview
PPT
asp.ppt for ajax fyjc semester 1 students
PPTX
Angular Framework ppt for beginners and advanced
Razor new view engine asp.net
Razor and the Art of Templating
ASP.NET MVC One Step Deeper
Asp.net With mvc handson
Super billing asp.net
ASP.NET MVC introduction
Overview of MVC Framework - by software outsourcing company india
Asp.net mvc
java-scriptcdvcx vnbm,azsdfghjkml;sxdfcgmndxfcgvhb nmfctgvbhjnm ,cfgvb nm,xc ...
CSC PPT 12.pptx
SCDJWS 5. JAX-WS
Jsplecture
Java ppt
Create an application with ember
Simple mvc4 prepared by gigin krishnan
Salesforce Lightning Web Components Overview
asp.ppt for ajax fyjc semester 1 students
Angular Framework ppt for beginners and advanced
Ad

More from Fajar Baskoro (20)

PPTX
Pengembangan Basis Data untuk Web Application.pptx
PPTX
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
PPTX
Sosialisasi Program Digital Skills Unicef 2025.pptx
PDF
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
PDF
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
PDF
Pemrograman Mobile menggunakan kotlin2.pdf
PPTX
Membangun Kewirausahan Sosial Program Double Track.pptx
PPTX
Membangun Kemandirian DTMandiri-2025.pptx
PDF
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
PDF
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
PPTX
Seleksi Penerimaan Murid Baru 2025.pptx
PPTX
Pengembangan Program Dual Track 2025-2.pptx
PPTX
Pengembangan Program Dual Track 2025-1.pptx
PDF
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
PPTX
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PPTX
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
PPTX
Program Dual Track Kalimantan Timur 2025.pptx
PDF
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
PPTX
Pengembangan Program Digital Skills - 2025.pptx
PPTX
PPT-Proyek Magang Kewirausahaan Double Track.pptx
Pengembangan Basis Data untuk Web Application.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Sosialisasi Program Digital Skills Unicef 2025.pptx
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Pemrograman Mobile menggunakan kotlin2.pdf
Membangun Kewirausahan Sosial Program Double Track.pptx
Membangun Kemandirian DTMandiri-2025.pptx
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
Seleksi Penerimaan Murid Baru 2025.pptx
Pengembangan Program Dual Track 2025-2.pptx
Pengembangan Program Dual Track 2025-1.pptx
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
Program Dual Track Kalimantan Timur 2025.pptx
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Pengembangan Program Digital Skills - 2025.pptx
PPT-Proyek Magang Kewirausahaan Double Track.pptx

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Institutional Correction lecture only . . .
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
STATICS OF THE RIGID BODIES Hibbelers.pdf
Presentation on HIE in infants and its manifestations
Institutional Correction lecture only . . .
Microbial diseases, their pathogenesis and prophylaxis
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Cell Structure & Organelles in detailed.
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf

Asp 4-razor-introduction of view engine

  • 1. Introduction of View Engine There are two types of view engine supported by ASP.NET MVC. The Razor and ASPX view engine View Engines are responsible for rendering the HTML from your views to the browser. The view engine template will have different syntax for implementation. What is @Razor ? Razor using an @ character instead of aspx's <% %> and Razor does not require you to explicitly close the code-block, this view engine is parsed intelligently by the run-time to determine what is a presentation element and what is a code element. Razor view engine is compatible with unit testing frameworks. This is the default view engine in MVC 3 and MVC 4. Web pages with Razor syntax have the special file extension cshtml (Razor with C#) or vbhtml (Razor with VB). What is <%ASPX%> ? This is the default view engine for MVC 1 and MVC 2. The syntax for writing views with this engine is the same syntax that the ASP.NET Web Forms uses and the file extensions are also taken from ASP.NET Web Form (.aspx, .ascx, .master) . Syntax and Uses of @Razor and <%ASPX%> It will be easy for ASP coder to learn about ASPX View Engine, and if you are comfortable with C# language then you can easily go with Razor view engine. Let's go through how we can use Razor and or ASPX view engine in our application. Let's start reviewing some of the usages. In this article I am try to cover both syntax with each other by grouping, this help us to understand and compare the syntax also it will help to you to remember easily. Comment in ASPX and Razor View Engine Comments are delimited in Razor syntax by @* *@ where in ASPX comments are delimited by <%-- --%> ? 1 2 @*Your Razor Comment Here*@ <%--Your Razor Comment Here--%>
  • 2. In a C# code block the comment for Razor or ASPX will be // and /* */ comment delimiters. Following code shows comments inside a c# code block. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @{ //Razor //Single line comment /* Multi line comment */ } <% //ASPX //Single line comment /* Multi line comment */ %> Code Block in ASPX and Razor View Engine Writing a server side code blocks in the source html is shown below for Razor ? 1 2 3 4 5 @{ string strVariable = "Razor Stirng"; bool isRazor = true; string[] arrRazor = new string[] { "MVC", "RAZOR", "AXPS"}; } The above code declares a string variable, bool variable and a array variable in Razor and the variables are got initialized with appropriate data in it. This code block is written in .cshtml file for Razor with C# and .vbhtml file for Razor with VB (must use VB syntax). The same code is written in .aspx (ASPX C#). ? 1 2 3 4 5 <% string strVariable = "Razor Stirng"; bool isRazor = true; string[] arrRazor = new string[] { "MVC", "RAZOR", "AXPS"}; %>
  • 3. Razor and ASPX with HTML Markup Following code block shows how we can integrate the Razor Code block with html markup when working in a dynamic web page we often required mixing code and html tags. ? 1 <span>@Your Code</span> The above code is written for Razor(C#) and the below code will acheive the same in ASPX(C#) view engine. ? 1 <span><%:YourCode %></span> There may be some other cases , in which we can marge HTML tag with Razor or ASPX view engine. ? 1 2 3 4 5 6 7 8 @if (isRazor) { <span>ViewEngine:Razor</span> } else { <span>ViewEngine:Other</span> } The above code used with ASPX view engine will be. ? 1 2 3 4 5 6 7 8 <%if (isRazor) { <span>ViewEngine:Razor</span> } else { <span>ViewEngine:Other</span> } %> You can do more mixing of HTML tags with Razor and ASPX View Engine, Following code block shows how you can do that. ? 1 2 Your Sample Html Code or Text @RazorCode (@AnotherRazorCode) Your Sample Html Code or Text <%: ASPXCode %> (<%:AnotherASPXCode %>)
  • 4. When the above code will rendered with the value of RazorCode and AnotherRazorCode variable is replaced as we do in server side coding. Similarly you can assign dynamic HTML attribute for any HTML tags. Let’s understand by an example. ? 1 2 <input type="checkbox" checked="@isRazor"> <input type="checkbox" checked="<%:isASPX%>"> Conditional Statement in ASPX and Razor View Engine Below is a sample Conditional statement for Razor (C#). ? 1 2 3 4 5 6 @if(isRazor) { <p>View Engine:Razor</p> } else { <p> View Engine:Other</p> } Below is a sample Conditional statement for ASPX (C#). ? 1 2 3 4 5 6 7 8 <% if (isASPX) { %> <p> View Engine:ASPX </p> <% } else { %> <p> View Engine:Other </p> <% } %> The above usage gives us a very easy approach for rendering things conditionally. Looping Statement in ASPX and Razor View Engine The below statement in Razor view engine is used for rendering the ListItem . ? 1 2 3 4 5 6 <ul> @foreach (var item in ListItem) { <li>@item</li> } </ul>
  • 5. Razor view engine is smart enough to identify the tag as an <li> html tag and the @item as a server side value. The <li> will be rendered multiple times with the list of products. ? 1 2 3 4 5 6 <ul> <%foreach (var item in ListItem) { %> <li><%=item%></li> <%} %> </ul> Calling Server Side in Method in ASPX and Razor View Engine To understand to call let’s assume that we have a static method named ViewEngineMethod in a class and called inside a tag using the appropriate view engines syntax. ? 1 <p>@{RazorASPXViewEngine.Models.SampleUtility.ViewEngineMethod();}</p> This code will be rendering with the output returned from the ViewEngineMethod() ? 1 <p><%=RazorASPXViewEngine.Models.SampleUtility.ViewEngineMethod();%></p> Creating a Delegate in Method in ASPX and Razor View Engine The delegates Razor can be define as templates build by C# and HTML. Here we have used Func instead of using Func. and we have used the 'item' to get the value inside the Func<>. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @{ Func<string[], object=""> SomeList = @<ul> @foreach (string p in item) { <li>@p</li> } </ul>; } Some listing from delegate- @SomeList(ListItem); </string[],>
  • 6. Creating a method using ASPX and Razor View Engine The @functions block in Razor helps you to define utility functions directly in the view. ? 1 2 3 4 5 6 7 @functions{ public string myMethod() { return "My Method"; } } Name:@myMethod(); We should create the function only when the function is specific to the view and can't be done using other conditional statement. By using @function we can separate view specific logic from controller. Escape Character in ASPX and Razor View Engine To escape an @ symbol just add @@ and it will render a single @ symbol. Razor language parser is very smart to konw the @ symbol is used for a static content or dynamic content. Let's take an example to understand this. ? 1 2 3 @@@ViewEngine @@ViewEngine example@DotNet-Tutorial.com The fist line will print @C# where as the second line will print @@ViewEngine. The third one will be parsed intelligently by the parser and will print the email address example@DotNet- Tutorial.com. The second line printed @@ViewEngine because of the escape symbol the variable will not be considered/evaluated. Summary: Razor view engine is widely used because of the lightweight syntax, readability, maintanability, TDD, and more. Razor uses @ symbol whereas ASPX uses <% %>. File extensions are different for Razor (C#-cshtml, VB-vbhtml) and ASPX(aspx, ascx, master) . Namespace for Razor is System.Web.Razor and for ASPX it's System.Web.Mvc.WebFromViewEngine. Razor sytax is very clean and easy to learn than ASPX view engine.