Razor tries to learn engine syntax (2)

Source: Internet
Author: User

Previous ArticleArticleThis article introduces the razor syntax ..

Similar to the webform engine, the razor syntax also allows some serversCodeEmbedded on the web page. The client code mainly includes HTML, CSS, and JavaScript. When there is code to be parsed by the server on the page, the razor syntax code will be dynamically compiled into HTML tags and returned to the client.

The web form engine file must be aspx or ascx suffix, And the razor file must be cshtml or vbhtml suffix. The server uses the file suffix to determine whether the current file uses the razor syntax.

Razor is also a new language based on ASP.net. Its relationship with Asp.net and. NET Framework platforms is as follows:

So if your website is currently using Asp.net, you can not apply it now. use aspx and ascx pages. on the cshtml or vbhtml page, I only know that in Asp.net MVC, I only need to register the corresponding attempt engine. Traditional Asp.net websites do not have much knowledge about how to use several Try engines at the same time.

1. More concise combination of text, HTML and code sets

Example:

The above is the code using the razor engine. If you use webform to try the engine, you need to use the <%> flag to aggregate the Code with HTML, but from the code and HTML together, @ compliance is less than <%>, however, if the number of lines in the Code is too large and the curly braces used for the IF else judgment need to be <%>, you will feel a lot more concise about Razor.

Running effect:

2. Use of @: and use of the <text> MARK:

@: It is used to output text or tags that cannot find matching HTML tags. If you use @: for a single line, but for multi-line code display, you can use the text label to replace.

Example:

The running result is different from the Expected One ....

The same effect is achieved when the <text> tag is used. Does the RTM version remove this function ?? ,???

3. newline During Variable assignment.

You can assign values to variables in one row, or assign values to variables at the beginning of other rows, or use the escape symbol @. In the preceding example, values are correctly assigned in the following three situations, however, an error is reported directly when square brackets are enclosed.

4. Variable type:

The data type in razor is similar to that in C #. Many variable types in C # And the way variables are declared can be seen in razor ..... For example, the above stirng name = "Joe"; int COUNT = 5; and so on.

4. Obtain the file directory in raozr:

Similar to the webform engine, for example:

Running effect:

5. Condition judgment and loop usage

Conditional judgment includes if, switch, etc,

Let's take a look at the example using the if condition:

If it is in C #, if (showtoday) above can be displayed normally without braces, but in razor, if you do not add this braces, you will have a cup.

Use if... Else example:

In the above example, if --- else is used, which is the same as that in C #. if you carefully check whether there is a @: or the <text> mark I commented out, they are used to output text in razor code. Without HTML output.

Switch condition judgment example:

Running result:

Loop does not need to be replaced with for, foreach, and while.

For Loop:

  

Use of foreach:

6. Use of hyperlinks.

There is a href method in razor. It is mainly used to obtain the complete URL of the corresponding file in the IMG tag, link tag, and a tag.

For example:

Run:

In addition to these syntaxes, you can also use the containers in system. Generic. Collection in razor. For example, dictionary.

You can also directly capture exceptions in razor code.

  

BRS

Nic

  

  

  

  

  

  

  

  

 

 

 

 

 

 

 

 

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.