Razor syntax quick reference, Razor syntax reference
| Syntax/Example |
Razor |
Web Forms |
| Code block |
@{ int x = 123; string y = "because.";} |
<% int x = 123; string y = "because."; %> |
| Expression (Html Encoded) |
<span>@model.Message</span> |
<span><%: model.Message %></span> |
| Unencoded) |
<span> @Html.Raw(model.Message) </span> |
<span><%= model.Message %></span> |
| Combine text and Markup |
@foreach(var item in items) {<span>@item.Prop</span> } |
<% foreach(var item in items) { %><span><%: item.Prop %></span> <% } %> |
| Mixed code and plain text |
@if (foo) { <text>Plain Text</text> } |
<% if (foo) { %> Plain Text <% } %> |
| Mixed code and plain text (alternative) |
@if (foo) { @:Plain Text is @bar } |
Same as above |
| Email Address |
Hi philha@example.com |
Razor recognizes basic email formats and does not use @ as the Code separator. |
| Explicit expression |
<span>ISBN@(isbnNumber)</span> |
In this case, parentheses must be used for the expression. |
| Cancel escape @ symbol |
<span>In Razor, you use the @@foo to display the value of foo</span> |
@ Indicates @ |
| Mixed expressions and text |
Hello @title. @name. |
Hello <%: title %>. <%: name %>. |
| Server comment |
@* This is a server side multiline comment *@ |
<%-- This is a server side multiline comment --%> |
Reference: http://blog.sina.com.cn/s/blog_55e55fcb0100o5de.html