Razor basic syntax 1: Razor basic syntax

Source: Internet
Author: User

Razor basic syntax 1: Razor basic syntax


  • What is Razor?
  • Rendering HTML
  • Razor syntax
  • Implicit Razor expression
  • Explicit Razor expression
What is Razor?

Razor is a markup syntax for converting server code to web pages. The syntax consists of the Razor tag, C #, and HTML. The main file that contains the Razor syntax is. cshtml.

Rendering HTML

The default language of Razor is HTML. rendering from Razor into an HTML is no different from the direct HTML. Razor contains the following code:

<P> I am a Chinese </p>

After the server is rendered or <p> I am a Chinese </p>, there is no difference.

Razor syntax

Razor supports C # and uses@Switch the symbol from HTML to C #, calculate the C # expression, and render it as HTML output. Razor can switch from HTML to C # Through the specified tag. When the @ symbol is followed by a reserved Razor word, it is switched to a specific tag of Razor. Otherwise, it is switched to a common C #.

If HTML needs to contain@You need to use@@Escape characters.


After rendering:


In this way, the email address @ symbol is not included in the HTML features and content, and the @ symbol is not processed as an escape character.

Implicit Razor expression

The implicit Razor expression starts"@"Symbol followed by C # code, for example:

Note: Implicit expressions cannot contain spaces. Otherwise, the analyzer reports the following error:

There is a space or line break behind the "@" character. Only valid identifiers, keywords, comments, and "(" and "{" are valid at the beginning of the code block, and they must be followed by "@" without spaces.

DisplayType Expression

An explicit expression that contains a pair of parentheses@Symbol, for example, rendering the last week on the page:

Any content in @ () is computed and rendered, and cannot contain spaces. For example, the following code:

After rendering:

2016/10/24 22:39:49-TimeSpan.FromDays(7)

You can also use @ {} to display the expression result to connect this point:

@{    var swich = new Person("Swich", 27);

If it is written as a non-display expression like @ (swich. Age), it is rendered as a mail address and

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.