Razor Syntax Samples

Implicit Code Expression

Code expressions are evaluated and written to the response. This is typically how you display a value in a view:


Code expressions in Razor are always HTML encoded.

Explicit Code Expression

Code expressions are evaluated and written to the response. This is typically how you display a value in a view:

<span>1 + 2 = @(1 + 2)</span>

Unencoded Code Expression

In some cases, you need to explicitly render some value that should not be HTML encoded. You can use the Html.Raw method to ensure that the value is not encoded.


Code Block

Unlike code expressions, which are evaluated and outputted to the response, blocks of code are simply sections of code that are executed. They are useful for declaring variables that you might need to use later.


int x = 123;

string y = “because.”;


Combining Text and Markup

This example shows what intermixing text and markup looks like using Razor.

@foreach (var item in items) {

<span>Item @item.Name.</span>


Mixing Code and Plain Text

Razor looks for the beginning of a tag to determine when to transition from code to markup.

However, sometimes you want to output plain text immediately after a code block. For example, the following sample displays some plain text within a conditional block.

@if (showMessage) {

<text>This is plain text</text>



@if (showMessage) { @:This is plain text.


Note that two different ways exist for doing this with Razor. The first case uses the special <text> tag. The tag

itself is a special tag and is not written to the response; only its contents are written out. I personally like this approach because it makes logical sense to me. If I want to transition from code to markup, I use a tag.

Others prefer the second approach, which is a special syntax for switching from code back to plain text, though this approach works only for a single line of text at a time.

Escaping the Code Delimiter

As you saw earlier in this chapter, you can display @ by encoding it using @@. Alternatively, you always have the option to use HTML encoding:


The ASP.NET Twitter Handle is @aspnet


The ASP.NET Twitter Handle is @@aspnet

Server-Side Comment

Razor includes a nice syntax for commenting out a block of markup and code.


This is a multiline server side comment.

@if (showMessage) {



All of this is commented out.


Calling a Generic Method

Calling a generic method is really no different from calling an explicit code expression. Even so, many folks get tripped up when trying to call a generic method. The confusion comes from the fact that the code to call a generic method includes angle brackets. And as you’ve learned, angle brackets cause Razor to transition back to markup unless you wrap the whole expression in parentheses.



Layouts in Razor help maintain a consistent look and feel across multiple views in your application.

If you’re familiar with Web Forms, layouts serve the same purpose as master pages, but offer both a simpler syntax and greater flexibility.








Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s