ASP.NET Web Pages - 對象


Web Pages 經(jīng)常是跟對象有關(guān)的。


Page 對象

您已經(jīng)看到了一些在使用的 Page 對象方法:

@RenderPage("header.cshtml")

@RenderBody()

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

If (isPost) {

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


某些 Page 對象方法

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


某些 Page 對象屬性

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


Page 對象的 Page 屬性

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

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

  • Page.Title

  • Page.Version

  • Page.anythingyoulike

頁面屬性是非常有用的。例如,在內(nèi)容文件中設(shè)置頁面標(biāo)題,并在布局文件中使用:

Home.cshtml

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


<h1>Welcome to W3CSchool.cc</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