ASP. net mvc razor view Engine

Source: Internet
Author: User

Razor is a new extension of Asp.net mvc3 and is also the default view engine.

CodeExpression

The core conversion character of razor is @, which is used to convert from tag to code (this is definitely a micro-controller, so it is concise and cannot be concise)

For example

 
@{StringName ="Zhangwei";}< H2 >@name </H2>

The razor code block can be automatically converted in the tag and code, such as automatic identification <li> </LI>, and {} can contain a code block.

 
<Ul> @Foreach(VARInStr) {<li >@a </Li >}</ul>

 

Solution

I want to output Zhangwei. xxiu, and the code becomes

@{StringName ="Zhangwei";}< H2> @ name. xxiu </H2>

 

Obviously. xxiu will be treated as an attribute of name, so a bracket is needed to solve the ambiguity.

 
@{StringName ="Zhangwei";}< H2 >@( name). xxiu </H2>

 

Use the dual @ number to address the @ content that needs to be output, such as email Weibo.

 
<P> chxxiu @ gmail.com </P> <a href ="Http://weibo.com/tlaozhang"> @ Zhang Wei-ProgramMember </a>

 

HTML Encoding

The razor expression uses HTML encoding.

For example

@{StringMassage ="<SCRIPT> alert ('Hello zhangwei') </SCRIPT>";}< H2 >@massage </H2>

If the input is normal, a dialog box will pop up, but the page is actually processed as follows:

 
<H2> & lt; script & gt; alert (& #39; Hello Zhangwei & #39;) & lt;/script & gt; </H2>

To output a dialog box class, try html. Raw.

 
<H2> @ html. Raw (massage) </H2>

Comment for trial

 
@ * Content to be commented *@
Razor Layout

We can see that in the template file _ viewstart. cshtml, there is only one sentence below. layout indicates the path that references the page layout. It refers to a default layout. We can modify this path as needed.

 
@ {Layout ="~ /Views/shared/_ layout. cshtml";}

 

In _layout.cs.html, there is the following code. The key is that @ renderbody () is used to hold the location of the main content. This is similar to the space left on the page in the previous template in webfrom.

<Div id ="Body"> @ Rendersection ("Featured", Required:False) <SectionClass="Content-wrapper main-content clear-fix"> @ Renderbody () </section> </div>

 

 

It is still very efficient to take notes while reading and Typing Code!

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.