let's take a look at the Code in the previous example:
@ p. name ($ @ p. price) . When the "
" Character Sequence is encountered, the parser knows that a tag with "" as the ending mark is being parsed. Then Mark the parser and find a "@" character before the resolution ends, just like "@ foreach", switch to the Code parser again. At this time, it is somewhat different from the previous parsing. When the C # code parser finds the first identifier "P", it will check whether the identifier is a keyword of C; of course, "P" is not a C # keyword, so the code parser enters the "implicit expression" mode. The algorithm for parsing implicit expressions looks like this:
- First read an identifier,
- The next character is "(" or "["?
- Yes, read the matched ")" or "]", and then jump to 2.
- If not, continue 3.
- The next character is "."?
- Yes. Continue 4.
- End expression if not
- "." Is the start of a valid C # identifier?
- Yes, read "." And jump to 1.
- If not, do not read "." and end the expression.
In general, an implicit expression is an identifier, which can be followed by any number of methods ("()") and index expressions ("[]"). and member access expression (". "). However, spaces are not allowed except in "()" or. For example, the following are some valid razor implicit expressions:
@ P. Name
@ P. Name. tostring ()
@ P. Name. tostring () [6-2]
@ P. Name. Replace ("aspx", "Razor") [I ++]
The following are some invalid expressions. Only some of these expressions ("=>") are considered as expressions by razor.
@ 1 + 1 ==> @
@ P ++ ==> @ P
@ P. Name => @ P
@ P. Name. Length-1 ==> @ P. Name. Length
This is why we need another expression syntax :"@(...) "Reason, we can put anything we want into" () "through this syntax. The above example uses this syntax to represent:
@ (1 + 1)
@ (P ++)
@ (P. Name)
@ (P. Name. Length-1)
Once the expression is verified, it is passed to the code generator. When it is @ foreach (){... } "When the code is generated, the code is written to the generated C # class file. For expressions (whether displayed or implicitly), this process is a little different. Unlike aspx, there is only one control structure "@", there is no "@ =" to distinguish between Running code and expressions that want to output values, but this is also the charm of razor. For example, when "@ foreach" is found, we know that "foreach" is a keyword in C #, so this block will be executed as a declaration, and "@ p. name "or" @ (1 + 1) ", we know they are expressions, so the execution results are output after these statements are executed.
In short:
@ If, @ switch, @ try, @ foreach, @ for, etc. are the same as "<%>"
@ P. Name, @ (P ++), @ (1 + 1), etc. are the same as "<%: %>"
Another thing to note is that the expression is equivalent to "<%:" instead of "<% = "". HTML encode processing should be performed by default in razor. If you do not want to perform HTML encode processing, you can use the ihtmlstring interface.
After knowing the parsing principle, let's go back to the previous Code:
<Li> @ P. Name ($ @ P. Price) </LI>
When "@ p. name ", you can identify this is an expression, through" ("parsing the space before the character is not a method call, followed by a text mark" "($ ", then, find "@" and then add "@ p. price "is parsed as an expression and ends.
View next article: razor insider Template
Click here to view the original text.
Note: If you find any translation inappropriate or missing, please submit it to me. I will correct it in time. Thank you!