”;
The most basic object in ASP.NET is the page. You can access properties of the page object directly without any qualifying object. In the previous chapters, we have used some of the properties and methods of page object like Layout, RenderPage and RenderBody. WebPageBase Class is the base class for classes that represent an ASP.NET Razor page.
Properties and Methods of Page Object Model
Following are some of the most commonly used properties of Page Object.
S.No | Property & Description |
---|---|
1 |
IsPost Returns true if the HTTP data transfer method used by the client is a POST request. |
2 |
Layout Gets or sets the path of a layout page. |
3 |
Output Gets the current TextWriter object for the page. |
4 |
Page Provides property-like access to data shared between pages and layout pages |
5 |
Request Gets the HttpRequest object for the current HTTP request. |
6 |
Server Gets the HttpServerUtility object that provides web-page processing methods. |
Following are some of the most commonly used methods of Page Object.
S.No | Method & Description |
---|---|
1 |
ConfigurePage When overridden in a derived class, configures the current web page based on the configuration of the parent web page. |
2 |
DefineSection Called by content pages to create named content sections. |
3 |
ExecutePageHierarchy() Executes the code in a set of dependent web pages. |
4 |
GetOutputWriter Returns the text writer instance that is used to render the page. |
5 |
href Builds a URL using the specified parameters |
6 |
InitializePage Initializes the current page. |
7 |
IsSectionDefined Returns a value that indicates whether the specified section is defined in the page. |
8 |
PopContext Returns and removes the context from the top of the OutputStack instance. |
9 |
PushContext Inserts the specified context at the top of the OutputStack instance. |
10 |
RenderBody() Renders the portion of a content page that is not within a named section (In layout pages) |
11 |
RenderPage(page) Renders the content of one page within another page |
12 |
RenderSection(section) Renders the content of a named section (In layout pages) |
13 |
Write(object) Writes the object as an HTML-encoded string |
14 |
WriteLiteral Writes an object without HTML-encoding it first. |
Let’s have a look into a simple example of Page property of Page Object which provides property-like access to data shared between pages and layout pages. In this example, we will set the title of the page using the Page.Title property.
Here is the implementation of MyLayoutPage.cshtml file in which we have set the page title.
@{ Layout = "~/_Layout.cshtml"; page.Title = "Layout Page"; } <h1> H1 Heading from the Layout page </h1> <p> This is the Main Body part from the Layout page</p>
Now we need to specify the same page title in the _Layout.cshtml page as shown in the following code.
@{ } <!DOCTYPE html> <html lang = "en"> <head> <title>@Page.Title</title> <link href = "@Href("/Styles/Site.css")" rel = "stylesheet" type = "text/css" /> </head> <body> @RenderPage("/Shared/_Header.cshtml") <div id = "main">@RenderBody()</div> @RenderPage("/Shared/_Footer.cshtml") </body> </html>
Let’s run the application and specify the following url − http://localhost:46023/MyLayoutPage then you will see the following page.
As you can see the title is now a Layout Page which we have set using the Page property of Page object.
Let’s have a look into another simple example in which we will use the Request Property of Page object
@{ Layout = "~/_Layout.cshtml"; Page.Title = "Layout Page"; var path = Request.FilePath; var pageUrl = this.Request.Url; } <h1> H1 Heading from the Layout page </h1> <p> This is the Main Body part from the Layout page</p> <a href = "@pageUrl">My page</a> <p>Page Url: @pageUrl</p> <p>File Path: @path</p>
You can get the page”s file path and URL using the Request object of the page. Let’s run your application again and you will see the following output.
”;