Syntax/Sample
Razor
Web Forms Equivalent (or
remarks)
Code Block
@{
<%
int x = 123;
int x = 123;
string y = "because.";
string y =
"because.";
%>
Expression (Html
<span>@model.Message</span
<span><%:
Encoded)
>
model.Message
%></span>
<span>
<span><%=
@Html.Raw(model.Message)
model.Message
</span>
%></span>
Combining Text and
@foreach(var item in items) {
<% foreach(var
markup
<span>@item.Prop</span>
item in items) { %>
Expression
(Unencoded)
<span><%:
item.Prop
%></span>
<% } %>
Mixing code and Plain
text
Mixing code and plain
text (alternate)
@if (foo) {
<% if (foo) { %>
<text>Plain Text</text>
Plain Text
<% } %>
@if (foo) {
Same as above
@:Plain Text is @bar
}
Email Addresses
Hi philha@example.com
Razor recognizes basic email
format and is smart enough
not to treat the @ as a code
delimiter
Explicit Expression
<span>ISBN@(isbnNumber)</sp
In this case, we need to be
an>
explicit about the expression
by using parentheses.
<span>In Razor, you use the
Escaping the @ sign
@@foo to display the value
@@ renders a single @ in the
response.
of foo</span>
@*
<%--
This is a server side
This is a server side
multiline comment
multiline comment
*@
--%>
@(MyClass.MyMethod<AType>())
Use parentheses to be
Server side Comment
Calling generic
method
explicit about what the
expression is.
@{
Creating a Razor
Delegate
Func<dynamic, object> b =
@<strong>@item</strong>;
}
Generates a Func<T,
HelperResult> that you can
call from within Razor. See
this blog post for more
details.
@b("Bold this")
Mixing expressions
Hello @title. @name.
and text
Hello <%: title %>.
<%: name %>.
Operators
Operator Description
+
*
/
Math operators used in
numerical expressions.
Examples
@(5 + 13)
@{ var netWorth = 150000; }
@{ var newTotal = netWorth * 2; }
@(newTotal / 2)
var age = 17;
Assignment. Assigns the value
on the right side of a statement
to the object on the left side.
==
!=
<
>
<=
>=
Equality. Returns true if the
values are equal. (Notice the
distinction between the =
operator and the == operator.)
Inequality. Returns true if the
values are not equal.
Less-than,
greater-than,
less-than-or-equal, and
greater-than-or-equal.
Concatenation, which is used
to join strings. ASP.NET
knows the difference between
this operator and the addition
operator based on the data type
of the expression.
+=
-=
()
[]
The increment and decrement
operators, which add and
subtract 1 (respectively) from a
variable.
Dot. Used to distinguish
objects and their properties and
methods.
Parentheses. Used to group
expressions and to pass
parameters to methods.
Brackets. Used for accessing
values in arrays or collections.
Not. Reverses a true value to
var myNum = 15;
if (myNum == 15) {
// Do something.
}
var theNum = 13;
if (theNum != 15) {
// Do something.
}
if (2 < 3) {
// Do something.
}
var currentCount = 12;
if(currentCount >= 12) {
// Do something.
}
// The displayed result is
"abcdef".
@("abc" + "def")
int theCount = 0;
theCount += 1; // Adds 1 to count
var myUrl = Request.Url;
var count =
Request["Count"].AsInt();
@(3 + 7)
@Request.MapPath(Request.FilePath);
var income =
Request["AnnualIncome"];
bool taskCompleted = false;
// Processing.
if(!taskCompleted) {
// Continue processing
and vice versa.
Typically used as a shorthand
way to test for false (that is,
for not true).
false
&&
||
Logical AND and OR, which
are used to link conditions
together.
bool myTaskCompleted = false;
int totalCount = 0;
// Processing.
if(!myTaskCompleted && totalCount <
12) {
// Continue processing.
}
Type Casting
Method
AsInt(),
IsInt()
AsBool(),
IsBool()
AsFloat(),
IsFloat()
Description Example
Converts a
string that
represents a
whole
number (like
"593") to an
integer.
Converts a
string like
"true" or
"false" to a
Boolean
type.
Converts a
string that
has a
decimal
value like
"1.3" or
"7.439" to a
floatingpoint
number.
var myIntNumber = 0;
var myStringNum = "539";
if(myStringNum.IsInt()==true){
myIntNumber = myStringNum.AsInt();
}
var myStringBool = "True";
var myVar = myStringBool.AsBool();
var myStringFloat = "41.432895";
var myFloatNum = myStringFloat.AsFloat();
AsDecimal(),
IsDecimal()
AsDateTime(),
IsDateTime()
Converts a
string that
has a
decimal
value like
"1.3" or
"7.439" to a
decimal
number. (In
ASP.NET, a
decimal
number is
more precise
than a
floatingpoint
number.)
Converts a
string that
represents a
date and
time value
to the
ASP.NET
var myStringDec = "10317.425";
var myDecNum = myStringDec.AsDecimal();
var myDateString = "12/27/2010";
var newDate = myDateString.AsDateTime();
DateTime
type.
ToString()
Converts
any other
data type to
a string.
int num1 = 17;
int num2 = 76;
// myString is set to 1776
string myString = num1.ToString() +
num2.ToString();
http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programmingusing-the-razor-syntax