This article is recorded by the scottgu's blog, with your own learning experience.
Original article address: Workshop. For future reference.
I. Razor
Razor is a new view engine that implements a fast and smooth coding process, making the view template concise and clean.
1: flexible options.
Add-> View... when creating a view template, you can select the razor (cshtml) template view or aspx (C #) template view.
2: Comparison of razor and aspx syntax
Variable variable:
In. aspx, hello: <% = Name %> does not need to be closed in razor.CodeBlock Hello: @ name
Loop and nesting:
<Ul> <% foreach (var p in Model) {%>
<Li> <% = P. ID %> </LI>
<% }%> </Ul>
In the razor engine:
<Ul> @ foreach (var p in Model ){
<Li> @ P. ID </LI>
} <Ul>
If statement code segment:
@ If (condition) {//...} else {//...}
If you want to output @, you can use @ to output @.
Eg: <p> what's your time? It's @ datetime. Now </P> what's your time? It's @ datetime. Now
In razor, HTML helper in the aspx engine is supported. Usage: @ html. textboxfor (M => M. Name)
3: Custom HTML helper that can be reused:
@ Helper themelist (int I)
{
If (I = 0)
// Do something ..;
Else // do 2;
}
Directly @ themelist (3)
3: @ Model
Dynamic type does not need to use inherite system. Web. MVC. iviewpag <...> directly use @ model strong type eg: @ model mvc3urlpro. Models. urlclass
4: loading the _ layout. cshtml and _ viewstart. cshtml pages
_ Viewstart. cshtml page: Specifies the template page for the page. You only need to add the _ viewstart page to the view folder, so you do not need to introduce the template page to each specific page.
_ Layout. cshtml page: the page for implementing the framework, that is, the template page.
Each time a page is opened, the advanced _ viewstart page runs the template page with the specified name based on the template page specified in the page. The default template page name is _ layout. cshtml.
5: Use @ * Comment statement * @ to comment on the server in razor *@
6: @: and <text> in razor
@: Explicitly specify a code block. For example: @ if (datetime. Now. Year = 2011 ){
@: This year is 2011 <br/> the date: @ datetime. Now
}
You can use multiple @: EG @: This year is 2011 to display multiple lines of code;
@: And it's a happy time;
You can also use the <text> </text> identifier in razor to display multi-line code blocks <text>
If (A = B)
{Displayed correctly ;}
Else Display error;
</Text>
7: Code expression @ and @()
Razor ParsingAlgorithm. Explicit @ () code is required
Eg: @ (A. sum ()/B. Count ()
8: _ layout template page and section in razor
@ Renderbody () on the _ layout template page: displays the body content of the page loaded by the page body.
@ Rendersection ("sectionname", required: True/false): The segments in the predefined template load the defined segments during page loading.
Eg @ section sectionname {
<P> This Is A Section One </P>
// Content ....
} The specific section definition is defined in the page.