"Razor" is a very good new view engine, which provides a fluent template solution focusing on code. Its coding workflow is fast, expressive, and interesting. The syntax is concise, saves the number of buttons, and increases the readability of the Code. ASP. NET's ASPX markup syntax uses "<% = %>" or "<%: %>" to mark "code fragments ", each code chunk must contain five characters to indicate the beginning and end position of the Code. In the Razor view engine, you only need to use a "@" character to identify the start of the code block, which is different from the "<%>" code chunk, razor does not require you to explicitly specify the end position of the code block.
1. Notes
1 @ * I am commenting 1 *@
2 <! -- I am commenting 2 -->
3 @{
4 // Note 3
5}
2. Execute the C # statement
1 <B> run the C # statement: </B>
2 @ DateTime. Now. Date. tow.datestring ()
3. Execute the C # code block
1 <B> run the C # code block: </B>
2 @ * C # syntax supported: Note ";" * @
3 @{
4 List <string> list = new List <string> () {"MVC3", "Razor "};
5 list. Add (". Net4 ");
6 list. Add ("");
7}
8 @ * Support for logical keywords such as foreach if else *@
9 @ foreach (string s in list)
10 {
11 if (string. IsNullOrEmpty (s ))
12 {
13 <li> null </li>
14}
15 else
16 {
17 <li> @ s </li>
18}
19}
20 @ * text output variable output *@
21 @{
22 @: @ list [0]
23 @: list [0]
24}
4. Use html to mark output variables and text
5. @ {...} Use @ internally to output variables and text in a single line. Otherwise, an error is returned.
6. Email and @
In most cases, the Razor parser has sufficient capabilities to export the "@" character in the template in code or static text. Razor analyzes the content on the right of the "@" character to determine whether the content is code or static text (the email is output as static content ).
1 @{
2 // output @
3 @: & #64
4 // output @
5 @:@@
6 // automatically recognized email output lhp0704@163.com
7 @: lhp0704@163.com
8}
7.@Url.Content
Converts a virtual (relative) path to an absolute application path. If the specified content path does not contain a negative number (~) Starts with a character. This method returns the unchanged contentPath.
1 <link href = "@ Url. Content ("~ /Content/Site.css ")" rel = "stylesheet" type = "text/css"/>
2 <script src = "@ Url. Content ("~ /Scripts/jquery-1.4.4.min.js ")" type = "text/javascript"> </script>
8. @ RenderSection ()
You can selectively define several different "sections" and allow the view templates designed based on this layout to overwrite them with custom content. This allows you to overwrite nonconsecutive sections on the layout design page in the view to make the layout design more flexible.
False indicates that the section Head can be disabled for a specific page.
1 @ * use the @ RenderSection method to preset a region and use it for the View that inherits this page in the future *@
2 @ RenderSection ("Head", required: false)
1 @ section Head {
2 <script type = "text/javascript">
3 $ (function (){
4 alert ("Hello World ");
5 });
6 </script>
9. @ RenderBody ()
Use the Helper function of "RenderBody ()" to place it in the template where you need to "fill in" The specific content based on the requested URL.
~ /Views/Shared/lower _ Layout. cshtml
1 <div>
Start 2
3 @ RenderBody ()
4. End
5 </div>
~ /Views/Home/Index. cshtml
1 @{
2 ViewBag. Title = "Homepage ";
3}
4 <p>
5. Place the content here @ RenderBody ().
6 </p>
10. @ RenderPage ()
~ /Views/Shared/lower _ Layout. cshtml
1 <div>
Start 2
3 @ RenderBody ()
4. End
5 <br/>
Start 6
7 @ * multiple times *@
8 @ RenderPage ("~ /Views/Home/About. cshtml ")
9. End
10 </div>
~ /Views/Home/About. cshtml
1 @{
2 ViewBag. Title = "About Us ";
3}
4 <p>
5. Place the content here.
6 </p>
11. Data Transmission
ViewData obtains or sets a dictionary that contains the data transmitted between the Controller and the view.
Obtain the View package using ViewBag.
TempData gets the temporary data to be passed to the view.
~ /Views/Home/Index. cshtml
1 @{
2 ViewBag. Title = "Homepage ";
3}
4 <p>
5. Place the content here @ RenderBody ().
6 </p>
~ /Controllers/HomeController. cs
1 public ActionResult Index ()
2 {
3 ViewBag. Message = "Welcome to ASP. net mvc! ";
4 ViewData ["Name1"] = "Razor view Engine 1 ";
5 TempData ["Name2"] = "Razor view Engine 2 ";
6 return View ();
7}
~ /Views/Shared/lower _ Layout. cshtml
1 <! DOCTYPE html>
2 3 4 <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
5 <title> @ ViewBag. Title </title>
6 7 <body>
8 <div>
9 @ RenderBody ()
10 @ ViewBag. Message <br/>
11 @ ViewData ["Name1"] <br/>
12 @ TempData ["Name2"]
13 </div>
14 </body>
15
Author Yixin yiyu