Razor syntax Learning

Source: Internet
Author: User

Original article: http://www.cnblogs.com/youring2/archive/2011/07/24/2115254.html

1. file type of Razor

Razor supports two file types:. cshtml and. vbhtml. The. cshtml server code uses the c # syntax, And the. vbhtml server code uses the vb.net syntax.

It can also be seen that Razor is actually a code template mixed with HTML code, similar to a. aspx file without post code.

2. @ symbol in Razor syntax

@ Is an important symbol in Razor. It is defined as the starting symbol of the Razor server code block. If you want to output a variable or the current date in the webpage, you can use the following code:

@{ productName = <span>@productName</span><span>@DateTime.Now.ToString()</span>

The @ symbol marks the output of an HTML code and outputs the variable, method return value, and expression operation result after it.

3. Definition of code blocks in Razor syntax

You can use @ {code} to define a piece of code block.

 num1 = num2 = sum = num1 +

In code blocks, we write code in the same way as the usual server-side code. In addition, if output is required, for example, the output result on the above page, we can use @ sum to complete the output.

In addition, @ (code) can output the calculation result of an expression. The above code can also be written as follows:

 num1 = num2 = sum = num1 ++

4. Mixed write of Razor syntax

Razor supports mixed code writing. You can insert HTML in a code block or a Razor statement in HTML.

 num1 = num2 = sum = num1 + color =<font color=>@sum</font>

  • Output @ Symbol :@@
  • Output Email address: the Razor template automatically identifies the Email address, so no conversion is required. In the code block, you only need to use @: Tom@gmail.com. @: Indicates that the following content is text.
  • Output HTML code (including tags): directly output, string html = "<font color = 'red'> text </font>"; @ html
  • Output HTML content (excluding tags): There are two methods: IHtmlString html = new HtmlString ("<font color = 'red'> text </font> "); @ html; Type 2: string html = "<font color = 'red'> text </font>"; @ Html. raw (html );

5. Comment on Razor syntax

The comment mentioned here refers to the comments on the server side. In the Razor code block, you can use the C # comments, which are // (single line comments) and/**/(multi-line comment ).

In addition, Razor provides a new server segment code comment, which can be C # code comments and HTML code comments ,@**@, this annotation method is not restricted by code blocks, and can be used anywhere in Razor code.

@ * <B> This is a comment </B> *@

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.