Let's take a look at the code in the previous example: how <li>@p.name ($@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:
@p.name
@p.name.tostring ()
@p.name.tostring () [6-2]
@p.name.replace ("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
@p.name.length–1 ==> @p.name.length
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)
@ (P.NAME.LENGTH-1)
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 "@p.name" or "@ (1 + 1)" is found, we know that they are expressions, So the execution results are output after these statements are executed.
Anyway:
@if, @switch, @try, @foreach, @for, etc. are the same as "<%%>".
@p.name, @ (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.name ($@p.price) </li> when "@p.name" 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 "."