ASP.NET Web Pages - Objects

Web Pages are usually related to objects.

Page object

You have already seen some used Page objects:

@RenderPage("header.cshtml")
@RenderBody()

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

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

Some methods of the Page object

Method Description
href Build a URL using the specified parameters.
RenderBody() In the layout page, the content page part that is not in the specified section will be displayed.
RenderPage(page) Present the content of a page within other pages.
RenderSection(section) In the layout page, the content of the specified section will be displayed.
Write(object) Write the specified object as a string encoded as HTML.
WriteLiteral You can write the specified object without first encoding it as HTML.

Some properties of the Page object

Property Description
isPost Returns a value (true or false) indicating whether the HTTP data transmission method used by the client to request the web page is a POST request.
Layout Get or set the path of the layout page.
Page Provide access to similar properties shared between pages, layout pages, and pagination.
Request Get the HttpRequest object for the current HTTP request.
Server Get the HttpServerUtility object, which provides methods that can be used during the web page processing process.

Page Properties (Page Object)

The Page properties of the Page object provide access to similar properties shared between pages, layout pages, and pagination.

You can add (use) your own properties like Page attributes:

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

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

Home.cshtml

@{
Layout="~/Shared/Layout.cshtml";
Page.Title="Home Page"
}
<h1>Welcome to CodeW3C.com</h1> 
<h2>Web Site Main Ingredients</h2>
<p>Home Page (Default.cshtml)</p>
<p>Layout File (Layout.cshtml)</p>
<p>Stylesheet (Site.css)</p>

Layout.cshtml

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