This page source address: http://www.cnblogs.com/dengxinglin/p/3352078.html
Razor is an Open-source project based on Framewor4 above: https://github.com/Antaris/RazorEngine/
The razor is comprised of a template engine and dynamic compilation in two parts. This section simply records some of the syntax of the template engine, then use the razor to do a code generator, using the razor syntax.
Razor's release is in conjunction with MVC, the View template engine for MVC.
Razor File Type
Razor can be used in vb.net and C #. Corresponding to the two file types,. vbhtml and. cshtml
identifier of the razor
The @ character is defined as the identifier for the Razor server code block, followed by the server code. In the Web form, use the <%%> to write server code. The VS tool provides code coloring and intelligent sensing functionality. Like the following code:
@{string username= "Dungshing";}
<span> @userName </span>
<span> @DateTime. now.tostring ("yyyy-mm-hh") </span>
Scope of Razor
In the previous example, the brace {} is used, and it is true that the braces contain a range of scopes, with a form like @{code} to write a block of code.
@{
string Username= "Dungshing";
@userName
}
The output in the scope (code block) is also in the @ symbol.
mixed with razor and HTML code
Writing razor statements in HTML code and HTML code in razor is OK, and there are smart hints.
A. Within the scope, as text output if it starts with an HTML tag
B. If you want to output @, use the @@
C. If you want to output code for a non-HTML tag and a razor statement, use @: and his role is equivalent to being written under HTML, such as @: can be followed by @ is a variable that represents a razor statement
Such as:
@{
var str = "abc";
Here is the output: This is a mail:dxl0321@qq.com, the This is Var:abc,this is mail@str,this are @ @: This is
a mail:dxl0321@qq.com , this is var: @str, this is mail@str,this is @@;
//below output ABC
@str
}
Razor Action block annotation
The razor block itself is the server code, so you can use the comments on the server code, and the comments have//and/**/are Single-line and multiline comments.
In addition, the razor annotation can also use the content *@ of its own @* annotation, which supports single-line and multiline.
@{
@*
multi-line annotation
multiline annotation
*@
var i = 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 ("~/")//represents the root directory of a Web site
@Html. Raw (module.content) output Html, such as: @Html. Raw (' <font color= ' red ' > Red Word </font> '), it will show the red "red word", This HTML string will be displayed directly (<font color= ' red ' > Red text </font>)
In practice, such as a Web site, the entire framework is the same, and some places are many of the same plate. So we need to reuse.
layout (Layout)
layout layout is equivalent to a template, we add code at its address place. Equivalent to defining the framework, as a master page, where the page below it needs to modify different code to use the @renderbody () method
<! DOCTYPE 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 a page, such as the head or tail of the public content. The output is using the @RenderPage () method
such as: A page will also be the content of the B page output
A page:
<p>
@RenderPage ("/b.cshtml")
</p>
The code for the B page is as follows:
<font color= "Red" > This is a sub page </font>
Section Area
A section is defined for use in layou. Used in Layout pages. Use @rendersection ("section name") in the parent page you want to layout
Defined:
<! DOCTYPE html>
Use in its subpages
@section submenu{
Hello This are section implement in about View.
}
If the submenu is not implemented in the subpage, an exception is thrown. We can overload it with @rendersection ("submenu", false)
@if (issectiondefined ("submenu"))
{
@RenderSection ("submenu", False)
}
else
{
< P>submenu section isn't defined!</p>
}
Helper
Helper is the definition of reusable helper methods that can be used not only on the same page but also on different pages.
As written in cshtml:
@helper sum (int a,int b)
{
var result=a+b;
@result
}
<div >
<p>@ @helper syntax