Detailed explanation of mvc3: Razor’s @: and syntax


This is another article in my blog series that covers some of the new features of ASP.NET MVC 3:

  • Layout in Razor October 22)
  • Server-side annotation with Razor (November 12)
  • Razor’s @: and < syntax > grammar (today)

This article will discuss two useful grammatical functions supported by the view engine in the new version of Razor: @: and < text > grammar.

Using Razor to Realize Fluent Programming

ASP.NET MVC 3 comes with a new view engine option called Razor (in addition to the existing. ASPX view engine). You can learn more about Razor on my blog introducing Razor, such as why we introduced it and the grammar it supports.

Razor minimizes the number of characters that need to be typed into a view template to achieve a fast and smooth programming workflow. Unlike the syntax of most templates, you don’t have to interrupt programming in HTML to clearly mark the beginning and end of a server-side statement block. Razor parsers are smart enough to infer from your code. This makes the grammar concise and easy to input cleanly, quickly and interestingly.

For example, the following code snippet iterates over a list of products:

One way Razor implicitly identifies when a code segment ends is to find tags or element content that represent the beginning of a content block. For example, in the code snippet above, Razor automatically treats the <li></li> module in the foreach loop as an HTML content block, because it sees the starting <li> tag sequence and knows that it is invalid in C#.

This unique technique — using tags to identify blocks of content in code — is an important factor in making Razor concise and efficient when it comes to HTML generation.

Use @ to clearly indicate the beginning of the content

However, not all content blocks start with tag elements. In some cases, Razor parsers cannot detect content blocks implicitly.

This requires Razor to explicitly indicate the beginning of the content block by using “@: character sequence” in the code block. @ The sequence indicates that the content behind the line should be treated as a content block:

Because I didn’t put the message “sold out!” Nested in HTML tag elements, Razor cannot implicitly determine whether the content in @if block is the beginning of a content block. We use “@: Character Sequence” to specify that this line in our code snippet should be treated as a content block.

Use Code Nugget in @: Content Block

In addition to outputting static content, you can also embed code fragments in content blocks starting with @: character sequences.

For example, in the following code snippet we have two @: character sequences:

Notice how we send the number of units in the content block in the second @: sequence (for example, “there are only 3 left!”). We implement this by embedding a @p.UnitsInStock code fragment in the content line.

Multi line content

Razor makes it easy to wrap multiline content in HTML elements. For example, the following content block in our @if container is wrapped in an HTML < p > element, which makes Razor think of it as content:

The < text > tag is a razor specific element. Razor regards the internal content of the < text > block as a content block and does not present the < text > label containing those contents (which means that only the internal content of < text > is presented, not the label itself). This makes it easy to render multi-line content blocks that are not wrapped by HTML elements.

If you prefer < text > elements to simpler @: sequences, < text > elements can also be used to identify single lines of content as needed:


Razor has enabled a concise template syntax to achieve a very smooth coding workflow. Razor can flexibly identify the beginning of content blocks by detecting < tag> elements, which makes Razor method very effective in HTML-generated scenarios, and also makes you not need to mark the beginning/end of content blocks obviously in 95% of if/else and foreach scenarios.

You can use Razor’s @: and < text > grammars when you don’t want to use HTML elements in a code container block but need to more accurately mark the boundaries of a content block.

I hope this will help. I also hope you will support developpaer more.

Recommended Today

Query SAP multiple database table sizes

Query SAP multiple database table sizes Item code db02 Here are two approaches, In the first graphical interface, the results of the query data table are displayed in MB, and only one table can be queried at a time. SPACE—Segments—Detailed Analysis—Detailed Analysis  In the pop-up window segment / object, enter the name of the […]