Menu
Code Block
Razor Syntax Cheat Sheet
This article covers all of the basics needed to use Razor within ASP.NET Web Pages, and should provide a ready reference for those using the Razor View Engine within ASP.NET MVC 3 onwards. If you would like to know more about how the Razor parser actually works, and track its evolution, you should visit the blog of Dr Razor - Andrew Nurse. For future reference you may find Phil Haack's Razor syntax cheat sheet useful. Share improve this answer follow answered Jun 27 '14 at 0:16. Rich O'Kelly Rich O'Kelly. 37.9k 8 8 gold badges 77 77 silver badges 107 107 bronze badges.
Expression (Html Encoded)
Expression (Unencoded)
Combining Text and markup
Mixing code and Plain text
Mixing code and plain text (alternate)
Email Addresses
Razor recognizes basic email format and is smart enough not to treat the
@
as a code delimiter.Explicit Expression
In this case, we need to be explicit about the expression by using parentheses.
Escaping the @
sign
@@
renders a single @
in the response.Server side Comment
Calling generic method
Use parentheses to be explicit about what the expression is.
Creating a Razor Delegate
Generates a
Func<T, HelperResult>
that you can call from within Razor. See[this post](http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx) for more details.Mixing expressions and text
NEW IN RAZOR v2.0/ASP.NET MVC 4
Conditional attributes
When
className = null
When
className = '
When
className = 'my-class'
Conditional attributes with other literal values
When
className
= nullNotice the leading space in front of
foo
is removed.When
className
= my-classConditional Data Attributes
data-*
attributes are always rendered.When
xpos = null or '
When
xpos = '42'
Boolean attributes
Examples
When
isChecked
= trueWhen
isChecked
= falseURL Resolution with tilde
Examples
When the app is at
/
When running in a virtual application named
MyApp
References
Razor is not a programming language. It's a server side markup language.
What is Razor?
Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages.
Server-based code can create dynamic web content on the fly, while a web page is written to the browser. When a web page is called, the server executes the server-based code inside the page before it returns the page to the browser. By running on the server, the code can perform complex tasks, like accessing databases.
Razor is based on ASP.NET, and designed for creating web applications. It has the power of traditional ASP.NET markup, but it is easier to use, and easier to learn.
Razor Syntax
Razor uses a syntax very similar to PHP and Classic ASP.
Razor:
<ul>
@for (int i = 0; i < 10; i++) {
<li>@i</li>
}
</ul>
@for (int i = 0; i < 10; i++) {
<li>@i</li>
}
</ul>
PHP:
<ul>
<?php
for ($i = 0; $i < 10; $i++) {
echo('<li>$i</li>');
}
?>
</ul>
<?php
for ($i = 0; $i < 10; $i++) {
echo('<li>$i</li>');
}
?>
</ul>
Classic ASP:
Rust Syntax Cheat Sheet
<ul>
<%for i = 0 to 10%>
<li><%=i%></li>
<%next%>
</ul>
<%for i = 0 to 10%>
<li><%=i%></li>
<%next%>
</ul>
Razor Helpers
ASP.NET helpers are components that can be accessed by single lines of Razor code.
You can build your own helpers using Razor syntax, or use built-in ASP.NET helpers.
Below is a short description of some useful Razor helpers:
- Web Grid
- Web Graphics
- Google Analytics
- Facebook Integration
- Twitter Integration
- Sending Email
- Validation
Razor Programming Languages
Razor supports both C# (C sharp) and VB (Visual Basic).