asp.net MVC Learning---(vii) Razor view engine syntax __.net

Source: Internet
Author: User
Tags html tags reflector

I've experienced the power of the Razor view engine @ symbol before.

Even my beginner can feel it. Saves time, labor, convenience, speed.

is a home travel * person * goods necessary AH (here Harmony two words ~)


So let's start with a further introduction to Razor's grammar.


1.c# statement block

Let's not say that.

@{Code}

C # code written between curly braces

The key is that you can also insert HTML tags directly between C # code

That's one of the big features of razor.

But how did it come to be identified?

is based on HTML tags.

or < angle brackets.

We could do a little test.

Add an action method named Razortest to the home controller and add a view (or do not select a master page)

The code in cshtml is as follows:

<body>
    @{for
        (int i = 0; i < 5; i++)
        {
            <div>~~~~</div>
        }
    }
</body >

Build request Razortest, the result is as follows:

Okay, this is going to be fine.

What if the code becomes this way.

<body>
    @{for
        (int i = 0; i < 5; i++)
        {
            <JChubby>~~~~</JChubby>
        }
    }
</body>

Jchubby is definitely not an HTML tag (the label you invented)

So what will happen.

Take a look.

It may not be intuitive to see the page directly, so let's go to the source code

It's amazing. It directly exports jchubby as an HTML tag to the browser.

What does that mean?

It shows that razor and HTML tags are not familiar at all.

It only knows < angle brackets ~.


2. Output data in email format

Because the @ symbol is the Razor keyword

So how do you output data in an email mailbox format with an @ symbol in the view?

Actually, it's simple.

Take a look at the following lines of code

<!--mailbox Format data-->
    <div>JChubby@qq.com</div>
    <!--output is jchubby+ variable qq.com-->
    < Div>jchubby @qq .com</div>
Did you find anything different here?

That's right

is a space relationship.

If you follow the @ symbol after a string, razor parsing does not treat it as a keyword, directly as a string

If the string is separated by a space only @, then the @ is the role of the keyword


3. Output string

Suppose that a variable str= "111" has been defined;

So

(1) @str

(2) @{response.write (str);

The results are the same in both ways


4. Output HTML string

Sometimes we want to spell an HTML string of our own and then output the display in the foreground

For example: String strhtml= "<input type= ' text '/>";

But if you use the @strhtml output directly,

Razor automatically escapes < angle brackets

The resulting final result is as follows:

It's obviously not the effect we're looking for, and what we need now is a text box

So how can you let razor not automatically escape it.

(1) Output HTML using Response.Write

But the downside is that Responsewrite's content is on top of the page.

(2) Use htmlstring output not to escape HTML

Such as:

htmlstring strhtml = new Htmlstring ("<input type= ' text '/>");

@strHtml

So we can meet our requirements.

Because Razor does not deal with htmlstring type of data

(3) Output not escaped HTML using the Html.raw method

Such as:

String strhtml = "<input type= ' text '/>";
@Html. Raw (strhtml)

The effect of this way is ditto

Because the raw method returns a Htmlstring object

(4) Using mvchtmlstring static method to create

Such as:

Htmlstring strhtml = mvchtmlstring.create ("<input type= ' text '/>");

Output does not escape the HTML



5. Define methods in the view

Define a method in a view that has a fixed format

As follows:

@helper Text (String a)

{

  Response.Write (a);

}

Must be a helper to start with and the return value is also a fixed helperresult type of data

In fact, it's rarely used to define methods directly in the view

Because the method is actually executing the business logic

The purpose of MVC is to keep views and business logic separate

The reason razor supports defining methods in views is that there may be a large number of duplicate required HTML code that needs to be exported

So you can implement HTML code reuse by defining ways



6. Where to use the method in the view

(1) A method that calls no return value

Call no return value method in view, you must add curly braces

For example, now there is a method with no return value Voidtext

When you are tuning in a view

@{voidtext ();}

(2) Calling a method that has a return value

Direct use of @test ()

Call test method (with return value)

and output the return value directly

(3) calling the generic method

Because the use of a generic method in C # uses the <> angle bracket

So it involves the problem of razor to the <> angle bracket parsing

So

When invoking a generic method, it is also wrapped with {} braces



7. Note

(1)

@* I am a multiline comment hahaha ... *@

(2)

@{
//I am a single-line comment hahaha ...
}


8. Output text in C # code

Attention OH ~

The text here is not the same as the previous output string ~

Previous is output string variable

The discussion here is to directly output the text

(1) Use Razor built-in label <text> output text ~~<text/>

(2) @: output of the text ~ ~ ~


9. Use @@ 两 symbols The final output effect is @


But is there a doubt now?

Razor tried to get the engine to compile the parse view.

Reflector can tell us the answer (it's an artifact.) )

We write the above code in the page to output the compiled assembly location

Copy the path where the assembly is located, paste it in the File Manager Address bar, locate the assembly, and drag into the reflector (note that the name of the assembly after each build is different)

Now you're looking at 4 compiled foreground view classes.

Open one casually (this is index).

To make it easier to see, cut the code into the Paint panel

Compared with the view source before compiling

Is it possible to find something ~.

That's right

The foreground class prints all the code in the page as an HTML string through a Excute method

It's just different processing for C # code and HTML code.

This also explains why the method is defined in the view to use the @helper and not to define a method like normal C #

Because the method (Excute) how can in the definition method.



OK, basically fix it ~

The syntax commonly used in the Razor view engine is probably the one mentioned above

If there is any omission or different understanding of the place Welcome to add ~ ~


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.