SlideShare a Scribd company logo
2
Most read
Asp.Net MVC - Razor Syntax
Asp.Net MVC - Razor Syntax
The Razor syntax also supports the Visual Basic language. 
•Razor code blocks are enclosed in @{ ... } 
•Inline expressions (variables and functions) start with @ 
•Code statements end with semicolon 
•Variables are declared with the var keyword 
•Strings are enclosed with quotation marks 
•C# code is case sensitive 
•C# files have the extension .cshtml
@{ int x = 123; string y = "because."; } 
<% int x = 123; string y = "because."; %> 
<span>@model.Message</span> 
<span><%: model.Message %></span> 
<span> 
@Html.Raw(model.Message) 
</span> 
<span> 
<%= model.Message %> 
</span> 
@foreach(var item in items) { 
<span>@item.Prop</span> 
} 
<% foreach(var item in items) { %> 
<span><%: item.Prop %></span> 
<% } %> 
@if (foo) { 
<text>Plain Text</text> 
} 
<% if (foo) {%> 
Plain Text 
<% } %>
@using (Html.BeginForm()) { 
<input type="text" value="input here"> 
} 
<% using (Html.BeginForm()) { %> 
<input type="text" value="input here"> 
<% } %> 
@if (foo) { 
@:Plain Text is @bar 
} 
Same as above 
Hi philha@example.com 
Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter 
<span>ISBN@(isbnNumber) </span> 
In this case, we need to be explicit about the expression by using parentheses. 
<span>In Razor, you use the @@foo to display the value of foo</span> 
@@ renders a single @ in the response.
@* 
This is a server side multiline comment 
*@ 
<%-- This is a server side multiline comment --%> 
@(MyClass.MyMethod<AType>()) 
Use parentheses to be explicit about what the expression is. 
@{ 
Func<dynamic, object> b = 
@<strong>@item</strong>; 
} 
@b("Bold this") 
Generates a Func<T, HelperResult> that you can call from within Razor. 
Hello @title. @name. 
Hello <%: title %>. <%: name %>.
<div class="@className"></div> 
When className = null 
<div></div> 
When className = "" 
<div class=""></div> 
When className = "my-class" 
<div class="my-class"></div> 
<div class="@className foo bar"> </div> 
When className = null 
<div class="foo bar"></div> 
Notice the leading space in front of foo is removed. When className = "my-class" 
<div class="my-class foo bar"></div> 
<div data-x="@xpos"></div> 
When xpos = null or "" 
<div data-x=""></div> 
When xpos = "42" 
<div data-x="42"></div>
<input type="checkbox" checked="@isChecked" /> 
When isChecked = true 
<input type="checkbox" checked="checked" /> 
When isChecked = false 
<input type="checkbox" /> 
<script src="~/myscript.js"> </script> 
When the app is at / 
<script src="/myscript.js"> </script> 
When running in a virtual application named MyApp 
<script src="/MyApp/myscript.js"> </script>
Asp.Net MVC - Razor Syntax
Asp.Net MVC - Razor Syntax
Ad

Recommended

PPTX
Introducing Razor - A new view engine for ASP.NET
Shiju Varghese
 
PDF
BEM it! Introduction to BEM
Varya Stepanova
 
PPT
JavaScript - Part-1
Jainul Musani
 
PPT
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
PDF
Magento 2 Design Patterns
Max Pronko
 
PDF
Files in java
Muthukumaran Subramanian
 
PPT
jQuery
Mostafa Bayomi
 
PDF
Struts Basics
Harjinder Singh
 
PPTX
CSS Positioning and Features of CSS3
Jaimin Brahmbhatt
 
PPTX
Introducing Cascading Style Sheets
St. Petersburg College
 
PPTX
Javascript 101
Shlomi Komemi
 
PDF
Intro to HTML and CSS basics
Eliran Eliassy
 
PPTX
Ajax presentation
Bharat_Kumawat
 
PPT
jQuery Ajax
Anand Kumar Rajana
 
PPTX
Tài liệu hướng dẫn sử dụng javascript cho lập trình web
PhanThanhToan1
 
PPTX
Html5 Basics
Pankaj Bajaj
 
PPTX
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
techlovers3
 
PPTX
Html5 semantics
Webtech Learning
 
PPT
What is OOP?
Amin Uddin
 
PPTX
Std 12 Computer Chapter 2 Cascading Style Sheets and Javascript(Part 1 CSS)
Nuzhat Memon
 
PPTX
Spring Web MVC
zeeshanhanif
 
PDF
Background property in css
Dhruvin Nakrani
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPT
Java script final presentation
Adhoura Academy
 
KEY
HTML CSS & Javascript
David Lindkvist
 
PDF
Introduction to HTML and CSS
Mario Hernandez
 
PPTX
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PDF
jQuery in 10 minuten
Anne Jan Roeleveld
 
PPTX
Introduction to ASP.Net Mvc3 with Razor
Manoj Kumar
 

More Related Content

What's hot (20)

PPTX
CSS Positioning and Features of CSS3
Jaimin Brahmbhatt
 
PPTX
Introducing Cascading Style Sheets
St. Petersburg College
 
PPTX
Javascript 101
Shlomi Komemi
 
PDF
Intro to HTML and CSS basics
Eliran Eliassy
 
PPTX
Ajax presentation
Bharat_Kumawat
 
PPT
jQuery Ajax
Anand Kumar Rajana
 
PPTX
Tài liệu hướng dẫn sử dụng javascript cho lập trình web
PhanThanhToan1
 
PPTX
Html5 Basics
Pankaj Bajaj
 
PPTX
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
techlovers3
 
PPTX
Html5 semantics
Webtech Learning
 
PPT
What is OOP?
Amin Uddin
 
PPTX
Std 12 Computer Chapter 2 Cascading Style Sheets and Javascript(Part 1 CSS)
Nuzhat Memon
 
PPTX
Spring Web MVC
zeeshanhanif
 
PDF
Background property in css
Dhruvin Nakrani
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPT
Java script final presentation
Adhoura Academy
 
KEY
HTML CSS & Javascript
David Lindkvist
 
PDF
Introduction to HTML and CSS
Mario Hernandez
 
PPTX
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
CSS Positioning and Features of CSS3
Jaimin Brahmbhatt
 
Introducing Cascading Style Sheets
St. Petersburg College
 
Javascript 101
Shlomi Komemi
 
Intro to HTML and CSS basics
Eliran Eliassy
 
Ajax presentation
Bharat_Kumawat
 
jQuery Ajax
Anand Kumar Rajana
 
Tài liệu hướng dẫn sử dụng javascript cho lập trình web
PhanThanhToan1
 
Html5 Basics
Pankaj Bajaj
 
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
techlovers3
 
Html5 semantics
Webtech Learning
 
What is OOP?
Amin Uddin
 
Std 12 Computer Chapter 2 Cascading Style Sheets and Javascript(Part 1 CSS)
Nuzhat Memon
 
Spring Web MVC
zeeshanhanif
 
Background property in css
Dhruvin Nakrani
 
Event In JavaScript
ShahDhruv21
 
Java script final presentation
Adhoura Academy
 
HTML CSS & Javascript
David Lindkvist
 
Introduction to HTML and CSS
Mario Hernandez
 
PHP Powerpoint -- Teach PHP with this
Ian Macali
 

Viewers also liked (20)

PDF
jQuery in 10 minuten
Anne Jan Roeleveld
 
PPTX
Introduction to ASP.Net Mvc3 with Razor
Manoj Kumar
 
PPTX
ASP.NET MVC Tips, Tricks and Hidden Gems
Shay Friedman
 
PDF
The Big Comparison of ASP.NET MVC View Engines
Shay Friedman
 
PPTX
Getting started with MVC 5 and Visual Studio 2013
Thomas Robbins
 
PPTX
Forest Cover Type Prediction
Rohit Arora
 
PPTX
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
Pureplexity
 
PPT
ASP
Ramasubbu .P
 
PPT
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
Pavel Tsukanov
 
PPT
ASP .net MVC
Divya Sharma
 
PDF
An introduction to knockout.js
Emanuele DelBono
 
KEY
Knockout.js presentation
Scott Messinger
 
PPTX
Getting Started with ASP.NET MVC 3 and Razor
Dan Wahlin
 
PPT
ASP.NET MVC Presentation
ivpol
 
PPT
MVC ppt presentation
Bhavin Shah
 
PPTX
Introduction to ASP.NET MVC
Khaled Musaied
 
PPT
An Introduction to Ajax Programming
hchen1
 
PPTX
Design for Developers: Introduction to Bootstrap 3
John Bertucci
 
PPT
Ajax Ppt
Hema Prasanth
 
PDF
jQuery Essentials
Marc Grabanski
 
jQuery in 10 minuten
Anne Jan Roeleveld
 
Introduction to ASP.Net Mvc3 with Razor
Manoj Kumar
 
ASP.NET MVC Tips, Tricks and Hidden Gems
Shay Friedman
 
The Big Comparison of ASP.NET MVC View Engines
Shay Friedman
 
Getting started with MVC 5 and Visual Studio 2013
Thomas Robbins
 
Forest Cover Type Prediction
Rohit Arora
 
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
Pureplexity
 
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
Pavel Tsukanov
 
ASP .net MVC
Divya Sharma
 
An introduction to knockout.js
Emanuele DelBono
 
Knockout.js presentation
Scott Messinger
 
Getting Started with ASP.NET MVC 3 and Razor
Dan Wahlin
 
ASP.NET MVC Presentation
ivpol
 
MVC ppt presentation
Bhavin Shah
 
Introduction to ASP.NET MVC
Khaled Musaied
 
An Introduction to Ajax Programming
hchen1
 
Design for Developers: Introduction to Bootstrap 3
John Bertucci
 
Ajax Ppt
Hema Prasanth
 
jQuery Essentials
Marc Grabanski
 
Ad

Similar to Asp.Net MVC - Razor Syntax (20)

PPTX
Introduction towebmatrix
Pranav Ainavolu
 
PPS
Razor new view engine asp.net
ahsanmm
 
PDF
Asp 4-razor-introduction of view engine
Fajar Baskoro
 
PPTX
Razor into the Razor'verse
Ed Charbeneau
 
PDF
Murach: ASP.NET Core MVC, How To Work With Razor Views
MahmoudOHassouna
 
PPTX
MVC & SQL_In_1_Hour
Dilip Patel
 
PPT
DDD Sydney 20111 Razor Session
Mohamed Meligy
 
PPSX
Session six ASP.net (MVC) View
Mustafa Saeed
 
PDF
WebMatrix, see what the matrix can do for you!!
Frédéric Harper
 
PDF
ASP.NET MVC 3
Buu Nguyen
 
PPTX
Intro to MVC 3 for Government Developers
Frank La Vigne
 
PPTX
Prairie Dev Con West - 2012-03-14 - Webmatrix, see what the matrix can do fo...
Frédéric Harper
 
PPTX
Building Websites Using ASP.NET Core Razor Pages
ssusere19c741
 
PDF
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
Frédéric Harper
 
PPTX
Intro to .NET for Government Developers
Frank La Vigne
 
PPTX
Asp.net With mvc handson
Prashant Kumar
 
PPT
Super billing asp.net
superb11b
 
PPTX
Asp.Net MVC 5 in Arabic
Haitham Shaddad
 
PPTX
Views
Eyal Vardi
 
PPTX
Umbraco OktoberFest 2014
Jeavon Leopold
 
Introduction towebmatrix
Pranav Ainavolu
 
Razor new view engine asp.net
ahsanmm
 
Asp 4-razor-introduction of view engine
Fajar Baskoro
 
Razor into the Razor'verse
Ed Charbeneau
 
Murach: ASP.NET Core MVC, How To Work With Razor Views
MahmoudOHassouna
 
MVC & SQL_In_1_Hour
Dilip Patel
 
DDD Sydney 20111 Razor Session
Mohamed Meligy
 
Session six ASP.net (MVC) View
Mustafa Saeed
 
WebMatrix, see what the matrix can do for you!!
Frédéric Harper
 
ASP.NET MVC 3
Buu Nguyen
 
Intro to MVC 3 for Government Developers
Frank La Vigne
 
Prairie Dev Con West - 2012-03-14 - Webmatrix, see what the matrix can do fo...
Frédéric Harper
 
Building Websites Using ASP.NET Core Razor Pages
ssusere19c741
 
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
Frédéric Harper
 
Intro to .NET for Government Developers
Frank La Vigne
 
Asp.net With mvc handson
Prashant Kumar
 
Super billing asp.net
superb11b
 
Asp.Net MVC 5 in Arabic
Haitham Shaddad
 
Views
Eyal Vardi
 
Umbraco OktoberFest 2014
Jeavon Leopold
 
Ad

Recently uploaded (20)

PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
PPTX
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
PDF
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
PDF
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
PDF
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
DOCX
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
PDF
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
PPTX
arctitecture application system design os dsa
za241967
 
PDF
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
PPTX
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
PDF
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
PDF
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
PDF
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
PDF
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
PDF
Heat Treatment Process Automation in India
Reckers Mechatronics
 
PDF
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
PPTX
Simplify Insurance Regulations with Compliance Management Software
Insurance Tech Services
 
PPTX
declaration of Variables and constants.pptx
meemee7378
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
arctitecture application system design os dsa
za241967
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
Simplify Insurance Regulations with Compliance Management Software
Insurance Tech Services
 
declaration of Variables and constants.pptx
meemee7378
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 

Asp.Net MVC - Razor Syntax

  • 3. The Razor syntax also supports the Visual Basic language. •Razor code blocks are enclosed in @{ ... } •Inline expressions (variables and functions) start with @ •Code statements end with semicolon •Variables are declared with the var keyword •Strings are enclosed with quotation marks •C# code is case sensitive •C# files have the extension .cshtml
  • 4. @{ int x = 123; string y = "because."; } <% int x = 123; string y = "because."; %> <span>@model.Message</span> <span><%: model.Message %></span> <span> @Html.Raw(model.Message) </span> <span> <%= model.Message %> </span> @foreach(var item in items) { <span>@item.Prop</span> } <% foreach(var item in items) { %> <span><%: item.Prop %></span> <% } %> @if (foo) { <text>Plain Text</text> } <% if (foo) {%> Plain Text <% } %>
  • 5. @using (Html.BeginForm()) { <input type="text" value="input here"> } <% using (Html.BeginForm()) { %> <input type="text" value="input here"> <% } %> @if (foo) { @:Plain Text is @bar } Same as above Hi [email protected] Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter <span>ISBN@(isbnNumber) </span> In this case, we need to be explicit about the expression by using parentheses. <span>In Razor, you use the @@foo to display the value of foo</span> @@ renders a single @ in the response.
  • 6. @* This is a server side multiline comment *@ <%-- This is a server side multiline comment --%> @(MyClass.MyMethod<AType>()) Use parentheses to be explicit about what the expression is. @{ Func<dynamic, object> b = @<strong>@item</strong>; } @b("Bold this") Generates a Func<T, HelperResult> that you can call from within Razor. Hello @title. @name. Hello <%: title %>. <%: name %>.
  • 7. <div class="@className"></div> When className = null <div></div> When className = "" <div class=""></div> When className = "my-class" <div class="my-class"></div> <div class="@className foo bar"> </div> When className = null <div class="foo bar"></div> Notice the leading space in front of foo is removed. When className = "my-class" <div class="my-class foo bar"></div> <div data-x="@xpos"></div> When xpos = null or "" <div data-x=""></div> When xpos = "42" <div data-x="42"></div>
  • 8. <input type="checkbox" checked="@isChecked" /> When isChecked = true <input type="checkbox" checked="checked" /> When isChecked = false <input type="checkbox" /> <script src="~/myscript.js"> </script> When the app is at / <script src="/myscript.js"> </script> When running in a virtual application named MyApp <script src="/MyApp/myscript.js"> </script>