Latest web development tutorials

ASP.NET Web Pages Object

Web Pages are often associated with the object.


Page Objects

You've already seen some of the Page object methods in use:

@RenderPage("header.cshtml")

@RenderBody()

In the previous chapter, you have seen two Page object properties (isPost and Request):

If (isPost) {

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


Some Page object methods

方法 描述
href 使用指定的值创建 URL。
RenderBody() 呈现不在布局页命名区域的内容页的一部分。
RenderPage( page ) 在另一个页面中呈现某一个页面的内容。
RenderSection( section ) 呈现布局页命名区域的内容。
Write( object ) 将对象作为 HTML 编码字符串写入。
WriteLiteral 写入对象时优先不使用 HTML 编码。


Some object properties Page

属性 描述
isPost 如果客户端使用的 HTTP 数据传输方法是 POST 请求,则返回 true。
Layout 获取或者设置布局页面的路径。
Page 提供了对页面和布局页之间共享的数据的类似属性访问。
Request 为当前的 HTTP 请求获取 HttpRequest 对象。
Server 获取 HttpServerUtility 对象,该对象提供了网页处理方法。


Page objects Page properties

Page Page object attribute, that provides access to shared similar properties between pages and page layout data.

You can use the Page Properties (add) your own attributes:

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

Page properties are very useful. For example, set the page title in the content file and use the layout file:

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