This article page Source address: http:/www.cnblogs.com/dengxinglin/p/3352078.html
Razor is an open source project based on Framewor4 and above: https://github.com/Antaris/RazorEngine/
Razor is comprised of a template engine and a dynamic compilation of two parts. This section simply records some of the syntax of the template engine, and then makes a code generator with razor, using the razor syntax.
Razor's release is in conjunction with MVC, as the MVC View template engine.
Razor file types
Razor can be used in vb.net and C #. There are two file types, vbhtml and. cshtml, respectively.
identifier of the Razor
The @ character is defined as the identifier of the Razor server code block, followed by the server code. It is a good idea to use <%%> to write server code in Web Form. The function of code coloring and intelligent sensing is provided in the VS tool. As in the following code:
@{" Dungshing ";} <span> @userName </span> <span> @DateTime. now.tostring ("yyyy-mm-hh") </span >
Scope of Razor
In the above example, the curly braces {} are used, and it is good that the curly braces are the scope of the representation scope and write a block of code with a shape such as @{code}.
@{ " Dungshing "; @userName}
Output in scope (code block) is also used with the @ symbol.
Write in Razor and HTML code mix
It is possible to write razor statements in HTML code and HTML code in razor, and there are smart hints.
A. As text output in scope if you start with an HTML tag
B. If you want to output @, use the @@
C. If you want to output non-HTML tags and non-razor statements of code, then use @:, his role is equivalent to be written under the HTML, such as @: Can be followed by the @ is the variable that represents the Razor statement
Such as:
@{var str = "abc "; ////output below: This is a mail:[email protected], this is Var:abc,this are [email protected],this is @; @: this is a mail:[email protected], this is var: @str, this is [Email protected],this is @@; // below output Abc @str}
Razor Action block annotations
The razor action block itself is the server code, so you can use the server code comments, comments have//and/**/are single-line comments and multi-line comments.
In addition razor annotations can also use the content of their own unique @* annotations *@, supporting single-line and multiline.
@{@* Multiline Comment multi-line Comments *@ ten; @* asdfasf *@}
Razor Type Conversions
As series extension methods and is series extension methods
Asint (), Isint ()
Asbool (), Isbool ()
Asfloat (), Isfloat ()
Asdecimal (), Isdecimal ()
Asdatetime (), Isdatetime ()
ToString ()
@{ var i = "ten
Razor Other
@Href ("~/")//indicates the root directory of the Web site
@Html. Raw (module.content) output Html, such as:@Html. Raw (' <font color= ' red ' > Scarlet Letter </font> '), it will show the red "Scarlet Letter" , the HTML string is displayed directly (<font color= ' red ' > Red text </font>)
In practice, such as a Web site, the whole frame is the same, and some places are many of the same section. So we need to reuse it.
Layouts (layout)
layout mode is the same as a template, we add code to its address place. The equivalent of defining the framework, as a master page, where the page below it needs to modify different code places using the @renderbody () method
<!DOCTYPE html><HtmlLang= "en"><Head><meta charset= "Utf-8" Span style= "color: #0000ff;" >/> <title> My Site-@Page. Title</title > </head> <body>< Span style= "color: #000000;" > @RenderBody () </body ></html>
@{ Layout = "/layoutpage.cshtml"; Page.title = "Test page Oh";} <P>this is a layout test</P>
Pages (page)
page is used when it is necessary to output the contents of another razor file in one page, such as the public content of the head or tail. The output uses the @RenderPage () method
such as: A page also to the content of the B page output
A page:
<p> @RenderPage ("/b.cshtml")</P>
The Code for page B is as follows:
<color= "Red"> This is a sub-page </font>
Section Area
The section is defined in the layou used. In the layout page. Use @RenderSection ("sectionname ") in the parent page to layout
Defined:
<!DOCTYPE html><HtmlLang= "en"><Head><MetaCharSet= "Utf-8" << Span style= "color: #800000;" >title> My Site-@Page. Title</title> </head< Span style= "color: #0000ff;" >> <body>< Span style= "color: #000000;" > @RenderSection ("submenu") @RenderBody () body></< Span style= "color: #800000;" >html>
Use in its sub-pages
@section submenu{ Hello This is a section implement in about View.}
If the submenu is not implemented in the sub-page, an exception is thrown. We can overload it with @rendersection ("submenu", false)
@if (issectiondefined ("submenu")) { @RenderSection ("submenu", False) } else { <p >submenu section are notdefined! </P> }
Helper
Helper is the ability to define reusable helper methods that can be used not only in different places on the same page, but also on different pages.
As in cshtml, write:
@helper sum (int a,int b) { var result=a+b; @result }<<p>@ @helper syntax </p</div>
We usually put a type of helper in a separate cshtml file, and the file name is equivalent to a class name.
I put sum in the helpmath.cshtml file, then we use it in the cshtml above:
<p>[email protected] (2,3)</p<P>[email protected] (5,9)</p >
In addition, the system provides us with some helper columns to simplify the writing of HTML. These helpers are placed in the @html, which we can use conveniently:
<p> @Html. TextBox ("txtname")</P>
Razor Grammar Encyclopedia