|
Razor |
Web Form |
Implicit Code Expression |
<span>
@model.Message
</span>
|
<span>
<%: model.Message %>
</span>
|
Explicit Code Expression |
|
<span>ISBN<%: isbn %></span>
|
Unencoded Code Expression |
<span>
@Html.Raw(model.Message)
</span>
|
<span>
<%: Html.Raw(model.Message) %></span>
or
<span><%= model.Message %>
</span>
|
Code Block |
@{
int x = 123;
string y = ˝because.˝;
}
|
<%
int x = 123;
string y = "because.";
%>
|
Combining Text and Markup |
@foreach (var item in items) {
<span>Item @item.Name.</span>
}
|
<% foreach (var item in items) { %>
<span>Item <%: item.Name %>.</span>
<% } %>
|
Mixing Code and Plain Text |
@if (showMessage)
{
<text>This is plain text</text>
}
or
@if (showMessage) {
@:This is plain text.
}
|
<% if (showMessage) { %>
This is plain text.
<% } %>
|
Escaping the Code Delimiter |
My Twitter Handle is @@haacked
|
<% expression %> marks a code nugget.
|
Server-Side Comment |
@*
This is a multiline server side comment.
@if (showMessage) {
<h1>@ViewBag.Message</h1>
}
All of this is commented out.
*@
|
<%--
This is a multiline server side comment.
<% if (showMessage) { %>
<h1><%: ViewBag.Message %></h1>
<% } %>
All of this is commented out.
--%>
|
Calling a Generic Method |
@(Html.SomeMethod<AType>())
|
<%: Html.SomeMethod<AType>() %>
|