@ Use Rule note in Asp.net mvc3 razor Engine

Source: Internet
Author: User

In the project, the foreground is Asp.net mvc3, Razor engine (for introduction to razor, refer to: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx), deeply experienced the convenient and powerful razor engine. However, some problems have also been encountered during the encoding process, that is, the @ mark of razor. When should this mark be added or not?

First readCode(It makes no sense to describe the problem:

Code 1:

 1   <  Div  >  
2 For (INT I = 0; I < 3 ; I ++)
3 {
4 </Div > I < Div >
5 For (Int J = 0; j < 3 ; J ++)
6 {
7 If (J = 1)
8 {
9 <Div >
10 For (int K = 0; k < 5 ; K ++)
11 {
12 K ++;
13 <Span >
14 K
15 </ Span >
16 }
17 </ Div >
18 Break;
19 }
20 }
21 }
22 </ Div >

This section of code does not contain any @ tag. It must be a problem. Can you accurately determine where the @ tag should be added in this section of code? Click here to stop for a few minutes and fill in your answer :). Then, compare your answer with the following reference answer. If you have done so with certainty and are correct, congratulations! You have mastered this knowledge point. You can skip the following content.

Reference answer:

Reference answer

 1   <  Div  >  
2 @ For (INT I = 0; I < 3 ; I ++)
3 {
4 @: </Div > @ I < Div >
5 For (Int J = 0; j < 3 ; J ++)
6 {
7 If (J = 1)
8 {
9 <Div >
10 @ For (int K = 0; k < 5 ; K ++)
11 {
12 K ++;
13 <Span >
14 @ K
15 </ Span >
16 }
17 </ Div >
18 Break;
19 }
20 }
21 }
22 </ Div >

Dear friend, the wrong kids shoes are wooden and wooden. It doesn't matter if you do something wrong. I 've also made a mistake before. Here is a small rule (defined by yourself, not necessarily accurate. If you already have a special term, you can point it out ), after you have mastered this small rule, you can easily make it right.

The razor syntax has the block statement. This is a code segment. For example, we can use the using () the encapsulated code {} is called a block (segment/block). The block can also be nested. For example, in the above sample code, the code inside the for loop of the outermost layer (lines 2nd to lines 21st) is a block, and lines 4th is a code block, the code in the second for loop (lines from 5th to 20th) can also be called a block. The Division is not fixed. You can define the granularity by yourself.

There are two types of internal code blocks: HTML code blocks and C # (C # in this example, or other. NET languages. With the concept of block, the @ tag rule in razor is:

If the same code block (@ is not required for the HTML code block, which is excluded here) is similar to the same code block internally, you do not need to add the @ tag. Otherwise, you need to add the @ tag.

Use this rule to parse the above Code one by one. Before parsing, my block Division is as follows:

Rows 1-22, 2-21, 4th, 5-20, 7-19, 9-17, 10-16, 12, 13-15, and 14

As we can see from the inside out, the outer block of the second row is HTML code. Therefore, the outer block of the second row is @, and the outer block of the second row is the C # code block. Therefore, the outer block of the second row is not added, and so on until the second row, this line is a bit special. This involves tag matching in.

We know that vs can match HTML tags During encoding. This match only gives a warning and is not mandatory. After razor is used, it is mixed in C # (or other languages) the HTML Tag in must also be matched, but this match is carried out inside the code block, and it is also brutal. If it does not match, an error will occur. For example, the following code is passable:

 1   <  Div  >  
2 < Tr >
3 @ If (true)
4 {
5 </ Tr >
6 }
7 </ Div >

Error Page:

Just like this:

 1   <  Div  > 
2 < Tr >
3 @ If (true)
4 {
5 < Tr > </ Tr >
6 }
7 </ Div >

What if we encounter HTML splicing like the code at the beginning </div> <div>? razor provides two solutions: Add "@:" to the front, for example, @: </div> <div>. The first is @ HTML. raw, such as @ HTML. raw ("</div> <div> ");

After learning about this, the Code should look like this in line 1: @: </div> @ I <div>. There are no problems with line 2 and there are only 4th lines left, in this case, ignore the 4th rows and write the code according to the rules. Because the 2nd rows are C # code blocks, you do not need to add @ to the 5th rows @.

Well, there is only one last point left here. If you change row 4th in code to <div> I </div>, what should row 5th be like? If the HTML tags in razor are matched, razor can recognize them. In this case, you can ignore the matching HTML tag pairs.

Sometimes the rules are not omnipotent, and a few of them still need to be tried. Finally, let's say a little trick. You can change the background color of the razor code so that you can easily identify it:

You are welcome to leave a message to discuss and provide a better solution.

Related Article

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.