一、Razor語法
1、Razor的標識符
解釋:@字符被定義為Razor服務器代碼塊的標識符,后面的表示是服務器代碼了。web form中使用<%%>中寫服務器代碼一個道理。在vs工具里面提供了代碼着色和智能感應的功能。
@{ string userName = "啟超"; <span>我的名字叫:@userName</span> <span>我的出生日期:@DateTime.Now.ToString("yyyy-MM-dd");</span> }
2、Razor的作用域
解釋:在上面一個例子中都已經使用到了大括號{},不錯,大括號里面的就是表示作用域的范圍,用形如 “@{code}”來寫一段代碼塊。在作用域 “@(code)” 中輸出也是用@符號的。
Index.cshtml頁面:
我的年齡:
@{
int age = 25;
string sex = "男";
@age
}性別:@(sex)
3、Razor和Html混合編寫
解釋:
a.在作用域內容如果是以html標簽開始則視為文本輸出
b.如果輸出@,則使用@@
c.如果要輸出非html標簽和非Razor語句的代碼,則用 "@:" ,他的作用是相當於在處於html下面編寫一樣了,如果在 "@:" 后面加上@就表示Razor語句的變量,如下:
Index.cshtml頁面:
@{ var strzm = "abc"; @:this is a mail:2734796332@qq.com.this is var:@strzm,this is mail@strzm,this is @@ //輸出abc @strzm }
4、Razor類型轉換
解釋:As系列擴展方法和Is系列擴展方法(string類型才能轉)
AsInt(),IsInt()
AsBool(),IsBool()
AsFloat(),IsFloat()
AsDecimal(),IsDecimal()
AsDateTime(),IsDateTime()
ToString()
Index.cshtml頁面:
@{ string ss = "123"; } string轉int:@ss.AsInt()
5、Razor其他
解釋:
@Href("~/")//表示網站的根目錄
@Html.Raw('<font color='red'>紅字</font>')就會顯示出紅色的”紅字“,不用的話會直接顯示這段html字符串(<font color='red'>紅字</font>)
————————————————————————————————————————————
二、Razor引擎
1、布局(Layout)(@RenderBody()方法)
解釋:Layout方式布局就相當於一個模板一樣的,我們在它地址地方去添加代碼。相當於定義好框架,作為一個母版頁的,在它下面的頁面需要修改不同代碼的地方使用@RenderBody()方法。
母版頁:(~/Views/Layout/_SiteLayout.cshtml)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>我的網站 - @Page.Title</title> </head> <body> @RenderBody() </body> </html>
子頁面:(~/Views/Home/About.cshtml)
@{ Layout = "~/Views/Layout/_SiteLayout.cshtml"; } <h1> 關於我的網站 </h1> <p> 這是一些內容顯示在關於我們這個頁面,我們用的是SiteLayout.cshtml這個主頁母版頁。 <br /> 當前時間:@DateTime.Now </p>
2、頁面(@RenderPage()方法)
解釋:page當需要在一個頁面中,輸出另外一個Razor文件(頁面)的內容時候用到,比如頭部或尾部這些公共的內容時需要用到,用@RenderPage()方法
母版頁:(~/Views/Layout/_SiteLayout.cshtml)
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Simple Site</title> </head> <body> <!--頭部--> @RenderPage("~/Views/Layout/_header.cshtml") <!--底部--> @RenderPage("~/Views/Layout/_footer.cshtml") </body> </html>
公共頁:(~/Views/Layout/_header.cshtml)
<div id="header"> <a href="#">主頁</a> <a href="#">關於我們</a> </div>
3、Section區域(@RenderSection())
解釋:Section是定義在Layout的頁面中使用的。在Layout的頁面中用。在要Layout的父頁面中使用@RenderSection()方法。
母版頁:(~/Views/Layout/_SiteLayout.cshtml)
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Simple Site</title> </head> <body> <div id="left-menu"> @RenderSection("menu",true) </div> </body> </html>
公共頁:(~/Views/Layout/_menu.cshtml)
@{ Layout = "~/Views/Layout/_SiteLayout.cshtml"; } <h1> 關於我的網站 </h1> <p> 這是一些內容顯示在關於我們這個頁面,我們用的是SiteLayout.cshtml這個主頁母版頁。 <br /> 當前時間:@DateTime.Now </p> @section menu{ <ul id="sub-menu"> <li>菜單1</li> <li>菜單2</li> <li>菜單3</li> <li>菜單4</li> </ul> }
如果在子頁面中沒有去實現了menu了,則會拋出異常。我們可以它的重載@RenderSection("menu", false)
@if (IsSectionDefined("menu")) { @RenderSection("menu", false) } else { <p>menu Section is not defined!</p> }
4、Helper
@helper就是可以定義可重復使用的幫助器方法,不僅可以在同一個頁面不同地方使用,還可以在不同的頁面使用。
4.1、新建一個HelperMath.cshtml頁面
4.2、HelperMath.cshtml頁面寫方法
@*求和*@ @helper sum(int a, int b) { int result = a + b; @result }
4.3、Index.cshtml頁面調用
1+2=@HelperMath.sum(1, 2)<br />