ASP.NET Web Pages - Objects


Web Pages is often about objects.


Page Object

You have already seen some Page object methods in use:

@RenderPage("header.cshtml")

@RenderBody()

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

If (isPost) {

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


Some Page Object Methods

Method Description
href Creates a URL using the specified value.
RenderBody() Renders a part of a content page that is not within a named region of the layout page.
RenderPage(page) Renders the content of one page within another page.
RenderSection(section) Renders the content of a named region of a layout page.
Write(object) Writes an object as an HTML-encoded string.
WriteLiteral Writes an object without HTML encoding.


Some Page Object Properties

Property Description
isPost Returns true if the HTTP data transfer method used by the client is a POST request.
Layout Gets or sets the path of the layout page.
Page Provides property-like access to data shared between the page and the layout page.
Request Gets the HttpRequest object for the current HTTP request.
Server Gets the HttpServerUtility object, which provides web page processing methods.


Page Property of the Page Object

The Page property of the Page object provides property-like access to data shared between the page and the layout page.

You can use (add) your own properties to the Page property:

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

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

Home.cshtml

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


<h1>Welcome to example.com</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


Other Extensions