Razor basic syntax 1: Razor basic syntax
Directory:
- 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.
<p>@@Email</p>
After rendering:
<p>@Email</p>
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