[Translation] razor insider expressions

Source: Internet
Author: User
Tags html encode

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:

      1. First read an identifier,
      2. The next character is "(" or "["?
        1. Yes, read the matched ")" or "]", and then jump to 2.
        2. If not, continue 3.
      3. The next character is "."?
        1. Yes. Continue 4.
        2. End expression if not
      4. "." Is the start of a valid C # identifier?
        1. Yes, read "." And jump to 1.
        2. 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!

    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.