asp.net MVC Razor view engine __.net

Source: Internet
Author: User

. Use the @ character to add code to the page. Just as the traditional ASPX view of <%%> is the same.
The following code describes a single statement block, an inline code block, a multiline statement block

1 <!--simple block declaration two variables-->
 2 @{int num = ten;}
 3 @{string str = "Hello Cnblogs";}
 4 <!--inline expressions Enter the two variables that you just declared--> 5 <p> the value of the shape you just declared is
 : @num </p>
 6 <p> The character value you just declared is: @str </p >
 7 
 8 <!--multiple declaration code block-->
 9 @{
Ten     string strhello = "Hello, my name is Liang";
One     string now = DateTime.Now.ToString ();     the string SayHello = Strhello + "Current time is:" + now;  <p> Hello: @sayHello </p>

The operation effect is as follows:

If I add HTML tags to the output text.

String SayHello = Strhello + "<b> Current time is:</b>" + now;

The only difference with the above code is in the "Current time for:" The end of a few words added <b></b>, we all know that the B-tag is bold text display, so I look at the results of the operation first.

The results of the operation are:

"Hello: Hello, my Name Liang <b> the current time is </b>:2012-5-25 18:03:21"

It's not the same as imagined. Hey, it's okay. Here we have to introduce a new concept of HTML coding. asp.net will default to security considerations such as < >. @ and so on special character encodings. Why do this is to prevent SSH cross-site scripting attacks, you can search for information on Cross-site scripting attacks, today's focus of this article is not to talk about it. From the browser's HTML source, you can see what the source is.

<p> Greetings: Hello, my Name Liang &lt;b&gt; the current time is: &lt;/b&gt;2012-5-26 18:25:49</p>

It can be clearly seen that <b> is coded as &lt;b&gt; <b/> is encoded into:&lt;/b&gt; So it's just the result that's on the page.

The problem comes, the result is the way. and look down.

<p> Hello: @Html. Raw (SayHello) </p>

@Html. Raw () prevents automatic encoding ^ ^^ ^^ ^^

2. Code and Tag blending

@for (int i = 0; I <= i++) { 
    <span> @i line <br/></span>
 }

Run Result:

Have you found a problem: there must be spaces between numbers and numbers and line words, this is because the @ character is preceded by a null character when the compiler can recognize the code after the @ symbol is C # code, the same as C # after the end must follow the null character, otherwise in this case it is recognized as @i line I row this variable (because the compiler cannot find this variable so the error is not found.) ) O (∩_∩) o~. What if I have to output a string without a space? I'll tell you how good it is. Just a pair of parentheses.

@for (int i = 0; I <= i++) { 
    <span> @ (i) line <br/></span>
 }

Run the results and guess for yourself. If you can not guess this, (*^__^*) Xi hee ... Don't say it--/

3. Code mixed with plain text

<p> the first way </p>
 @if (True) { 
    <text> here is the IF statement internal -1</text>
 }
 <p> the second way </ P>
 @if (true) { 
    @: Here is the IF statement internal-2
 }

3.1 The first way is to use the tags built into the razor <text></text> note that this is not an HTML tag, his mapping to the browser is plain text display without any tags.

3.2 The second way is to use @: He says the stuff behind is text.

Operation Effect:

Additional HTML source file: Proof text label amount is not HTML tag

<p> the first way </p>
    here is if statement Internal-1
 <p> the second way </p>
    here is if statement Internal-2

4. Expressions mixed with text

<p> expression mixed with text </p>
 Hello @strHello. Current time: @now

This is a simple way of not doing too much explaining, the knowledge point is to ensure that the @ character is preceded by a null character and then the form end is also a null character.
Run results

5. Email address and @ symbol

<p>email address </p>
 wlit2011@126.com

The Razor view automatically recognizes the email address without special processing, but I'm just going to output an @ character. You say how to learn so many years of procedures, but also how to do a reversal of righteousness Bai.
The inverse method is to add an @ character in front of it, and you will recognize it as a @ character when you write the two @ character @@ 就

<p> I want to export powerful @@ 符号 </p>

Run results

6. The instance you just want to output is out of the page range so I'd like to temporarily comment out the previous demo code, so let's say how to comment.

A server-side comment is a comment on the code it differs from the client comment (<!---->), and the client's comment code is sent to the browser although it is not visible in the interface, but viewing the source file will see the drop. Instead, server-side comments are not sent to the browser. To the effect of the code comments, look at the following code is also a good review of the general knowledge.

1 

The annotation content is in the middle of the *@ (service-side comment end tag) from the @* (service-side comment start tag) on line 2nd and the output is 5th. The email address and the output of the @ symbol.

Format: @* annotation content *@ very similar to C # Multiline annotation mode (/*/* *)

7. Mixed code in content

Take a good look at the following code is a comprehensive application of the above knowledge

<p> content Mixed Code </p>
<ul>
 @for (int i = 0; I <= i++) { 
    <li>
        current loop to @ (i+1) Times
        @if (i = = 5) { 
            @: Wow. Traversal to the number 5
        }
        else if (i = = 9) { 
            @: Pro This is the second countdown to the cycle--i+1

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.