top
Loading...
ASP.NET Web Pages 對象

ASP.NET Web Pages - 對象


Web Pages 經常是跟對象有關的。


Page 對象

您已經看到了一些在使用的 Page 對象方法:

@RenderPage("header.cshtml")

@RenderBody()

在前面的章節中,您已經看到了兩個 Page 對象屬性(isPost 和 Request):

If (isPost) {

if (Request["Choice"] != null ) {


某些 Page 對象方法

方法 描述
href 使用指定的值創建 URL。
RenderBody() 呈現不在布局頁命名區域的內容頁的一部分。
RenderPage(page) 在另一個頁面中呈現某一個頁面的內容。
RenderSection(section) 呈現布局頁命名區域的內容。
Write(object) 將對象作為 HTML 編碼字符串寫入。
WriteLiteral 寫入對象時優先不使用 HTML 編碼。


某些 Page 對象屬性

屬性 描述
isPost 如果客戶端使用的 HTTP 數據傳輸方法是 POST 請求,則返回 true。
Layout 獲取或者設置布局頁面的路徑。
Page 提供了對頁面和布局頁之間共享的數據的類似屬性訪問。
Request 為當前的 HTTP 請求獲取 HttpRequest 對象。
Server 獲取 HttpServerUtility 對象,該對象提供了網頁處理方法。


Page 對象的 Page 屬性

Page 對象的 Page 屬性,提供了對頁面和布局頁之間共享的數據的類似屬性訪問。

您可以對 Page 屬性使用(添加)您自己的屬性:

  • Page.Title
  • Page.Version
  • Page.anythingyoulike

頁面屬性是非常有用的。例如,在內容文件中設置頁面標題,併在布局文件中使用:

Home.cshtml

@{
Layout="~/Shared/Layout.cshtml";
Page.Title="Home Page"
}


<h1>Welcome to </h1>

<h2>Web Site Main Ingredients</h2>

<p>A Home Page (Default.cshtml)</p>
<p>A Layout File (Layout.cshtml)</p>
<p>A Style Sheet (Site.css)</p>

Layout.cshtml

<!DOCTYPE html>
<html>
<head>
<title>@Page.Title</title>
</head>
<body>
@RenderBody()
</body>
</html


北斗有巢氏 有巢氏北斗