The expression of razor inside

Source: Internet
Author: User
Tags foreach continue end expression html encode tostring

Let's take a look at the code in the previous example: how <li> ($@p.price) </li> is parsed. When the "<li>" character sequence is encountered, the parser knows that it is parsing a tag with "</li>" as the end sign. The tag parser then discovers a "@" character before parsing to the end flag, and, like "@foreach," switches to the code parser again. This is somewhat different from the previous parsing, when the C # code parser discovers the first identifier "P", it checks that the identifier is not a C # keyword, and of course "P" is not a C # keyword, so the code parser goes into the implicit expression pattern. The algorithm for parsing an implicit expression looks like this:

First read an identifier,

The next character is "(" or "[").

Read the matching ")" or "]", and then jump to 2

No, then continue 3.

The next character is "."?

Yes then continue 4

Not the end of the expression

"." is the following character the beginning of a valid C # identifier?

Yes, read "." and jump to 1

No, you don't read "." and ending an expression

In general, an implicit expression is an identifier that can then be invoked with any number of method calls ("()"), an index expression ("[]"), and a member access expression ("."). )。 However, spaces are not allowed except in "()" or "[]". For example, here are some legitimate razor implicit expressions: () () [6-2] ("ASPX", "Razor") [i++]

Here are some illegal expressions that are only partially (after "==>") and are razor considered expressions.

@1 + 1 ==> @
@p++ ==> @p
@p. Name ==> @p–1 ==>

This is why we need another expression syntax: "@ (...)" The reason for this syntax is that we can put anything we want into the "()", and the example above is expressed in this syntax:

@ (1 + 1)
@ (p++)
@ (P. Name)

Once we validate the expression, we pass it to the code generator. When "@foreach () {...}" When the code is generated, the code is written to the generated C # class file. For an expression, whether it is displayed or implicit, this process is a bit different, unlike ASPX, where there is only one control structure "@" and there is no "@=" to distinguish between running code and expressions to output values, but this is also the charm of razor. For example, when "@foreach" is discovered, we know that "foreach" is a keyword in C #, so the block is executed as a declaration, and when "" or "@ (1 + 1)" is found, we know that they are expressions, So the execution results are output after these statements are executed.


@if, @switch, @try, @foreach, @for, etc. are the same as "<%%>"., @ (p++), @ (1 + 1), etc is the same as "<%:%>"

Another place to note is that the expression is equivalent to "<%:" rather than "<%=". HTML encode should be processed by default in Razor, and the Ihtmlstring interface can be used if you do not want HTML encode processing.

Once you know the principle of parsing, let's go back to the previous code:

<li> ($@p.price) </li> when "" is found, it is recognized that this is an expression, resolved by the "(") space before it is not a method call, followed by the text tag "" ($), Then the "@" is found again after "@p.price" resolves to an expression, and finally ends with "."

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.