📜  ASP.NET WP-页面对象模型

📅  最后修改于: 2020-11-21 05:29:48             🧑  作者: Mango


ASP.NET中最基本的对象是页面。您可以直接访问页面对象的属性,而无需任何限定对象。在前面的章节中,我们使用了诸如Layout,RenderPage和RenderBody之类的页面对象的某些属性和方法。 WebPageBase类是表示ASP.NET Razor页面的类的基类。

页面对象模型的属性和方法

以下是页面对象的一些最常用的属性。

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.

以下是页面对象的一些最常用的方法。

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.

让我们看一下页面对象的页面属性的简单示例,该示例提供对页面和布局页面之间共享的数据的类属性访问。在此示例中,我们将使用Page.Title属性设置页面的标题。

这是MyLayoutPage.cshtml文件的实现,我们在其中设置了页面标题。

@{
   Layout = "~/_Layout.cshtml";
   page.Title = "Layout Page";
}

H1 Heading from the Layout page

This is the Main Body part from the Layout page

现在,我们需要在_Layout.cshtml页面中指定相同的页面标题,如以下代码所示。

@{ }


   
   
      @Page.Title
      
   
   
   
      @RenderPage("/Shared/_Header.cshtml")
      
@RenderBody()
@RenderPage("/Shared/_Footer.cshtml")

让我们运行该应用程序并指定以下URL- http:// localhost:46023 / MyLayoutPage,然后您将看到以下页面。

布局Cshtm

如您所见,标题现在是一个布局页面,我们已经使用Page对象的Page属性设置了该页面。

让我们看一下另一个简单的示例,其中我们将使用Page对象的Request属性

@{
   Layout = "~/_Layout.cshtml";
   Page.Title = "Layout Page";
   var path = Request.FilePath;
   var pageUrl = this.Request.Url;
}

H1 Heading from the Layout page

This is the Main Body part from the Layout page

My page

Page Url: @pageUrl

File Path: @path

您可以使用页面的Request对象获取页面的文件路径和URL。让我们再次运行您的应用程序,您将看到以下输出。

标题页