Application objects and page lifecycle of Asp.net

Source: Internet
Author: User


After receiving a new http request, IIS will eventually call asp.net _ isapi. dll ISAPI extension (especially in the IIS6.0 environment, the iis7.0 application pool defaults to the integration mode, which is relatively changed), and then passed to httpRuntime Pipe (http runtime pipeline), Asp.. Net (HttpRunTime is Asp. net), HttpRunTime automatically creates an HttpApplication instance for each asp.net application, and the instance contains the following attributes:

Note 1

Application --> equivalent to the application object in the traditional asp era, it is usually used to define a global variable of an asp.net Application.

Context --> HttpContext (Context) class instance [added by Asp. Net]

Modules --> sets of HttpModule Modules that affect the current application

Request --> similar to the Request object in asp, it is usually used to receive certain values (such as Request. Form or Request. QueryString)

Response --> similar to the Response object in asp, it is usually used to output specified content to the page (such as Resonse. Write)

Server --> similar to the Server object in asp, it can obtain some Server information (such as Server. MapPath)

Session --> similar to the Session object in asp

User --> used to obtain security information related to User authentication


From the above attributes, we can find that many of them are actually used in the asp era. Only Context, Modules, and User are newly added by Asp. Net.


In addition to the attributes of "NOTE 1", the HttpApplication class also has its own methods. Here we will mention the Init and Dispose methods, which can be reloaded.

Their call time is:

The Init method is called after Application_Start, while Dispose is called before Application_End. In addition, Application_Start is only triggered once throughout the lifecycle of the asp.net application (for example, when IIS or website is started ), similar Application_End is called only when the asp.net application is closed (for example, when IIS or website is stopped)

In addition to the Application_Start and Application_End methods, HttpApplication also provides the following events:


These events include the reloaded Init and Dispose methods mentioned above, and the Session_Start and Session_End methods corresponding to the Session can be directly stored in Global. ascx. cs is used in the form of Application_XXX (because Global. ascx. the class Global defined in cs is inherited from HttpApplication)

The Code is as follows:
Public class Global: System. Web. HttpApplication

Next, let's take a look at the three attributes of Context, Modules, and User added relative to asp.

Context:

The Context class is an instance of the HttpContext class. In the lifecycle of almost the entire aspx page, Context is always passed down along with each link.

So we can use HttpContext in almost any part of the web application. current to reference the Current Context instance. From the definition of HttpContext, you can also find that the attributes of Context can also get Application, ApplicationInstance, Profile, Response. request... and so on.


Recall:

he Code is as follows:
Public class Handler1: IHttpHandler
{

Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
Context. Response. Write ("Hello World ");
}

Public bool IsReusable
{
Get
{
Return false;
}
}
}


When we use an ashx file, the ProcessRequest method is to pass in the current context, and then get the reference of the Response object through context, and finally output any desired content to the page.

Modules:

Every class that implements the IHttpModule interface can be considered as an Http module component. It can be understood as an http request interceptor. After an http request is intercepted, it can modify the Context being processed, after the completion, return the control to the pipeline. If there are other Modules, continue to process them until the HttpModule in all the Modules sets is "refreshed". (Note: the poor http request is sent to the httpModule round X)

Asp. net2.0 has many built-in httpmodules by default. The following default built-in modules can be found in the Machine. Config file:

Note 2
AnonymouseIdentification-assigns a temporary identity to anonymous users
FileAuthorization -- verify if the user has a Windows NT license for the requested file
FormsAuthentication-form Authentication Module (if this module is not available, asp.net will not be able to authenticate using the user name/password [FOrms] mode)
OutputCache-output cache Module
PassportAuthentication-PassPort verification module
Profile-user configuration module (if it is not available, Profile cannot be used in asp.net)
RoleManager-role management
SessionSate -- session Status Module
UrlAuthorization-URL-based authentication module
WindowsAuthentication-Authentication Module for Windows and IIS

User:

If you have used asp. net2.0's built-in Membership/Role mechanism for access authentication, you will be familiar with User objects, such:

The Code is as follows:
If (HttpContext. Current. User. Identity. IsAuthenticated)
{
// The user has logged on...
}


It is often used to determine the logon status of the current browser User. For more details about the User class, see the MSDN


Lifecycle:

Finally, let's review the lifecycle of the Page in Asp. Net. The Page defines several events:


In general, when An ASPX Page is requested, the final lifecycle is the above events defined in the Page (there are some reload-able callback methods) as well as the previously mentioned events defined in the HttpApplication class (with the corresponding callback method) jointly triggered or called, and finally formed a series of processing processes.

If you do not consider the event processing method in HttpApplication (that is, we do not consider the event processing method in Global. ascx. the Application_XXX processing method defined in cs). The general trigger (CALL) sequence of events (methods) in Page is as follows:

01. Page_PreInit

02. Page_Init

03. Page_InitComplete

04. Page_PreLoad

05. Page_Load

06. Page_LoadComplete

07. Page_PreRender

08. Page_SaveStateComplete

09. Page_Unload

This is the normal order of the Page subcontrol without sending back the Page. If you add the Page sending back (for example, put an asp: Button in the Page, then add the processing function to the Button's Click sending back event), the order changes slightly:

01. Page_PreInit

02. Page_Init

03. Page_InitComplete

04. Page_PreLoad

05. Page_Load

06. button#click

07. Page_LoadComplete

08. Page_PreRender

09. Page_SaveStateComplete

10. Page_Unload

The difference is that the callback event Button1_Click is triggered after Page_Load.

Finally, let's take the HttpApplication event into consideration and look at the overlapping order. But let's take a look at a special situation first. If the ghost page is not set in the root directory of an asp.net application, then, you can directly browse the root directory, for example, when http: // localhost: 2345/, Globl. ascx. the call sequence of the Application_XXX method defined in cs is as follows:
15:01:39 413 Application_Start

15:01:39 491 Init

15:01:39 491 Application_BeginRequest

2010-03-28 15:01:39 506 Application_AuthenticateRequest

15:01:39 506 Application_PostAuthenticateRequest

15:01:39 506 Application_AuthorizeRequest

15:01:39 522 Application_PostAuthorizeRequest

2010-03-28 15:01:39 522 Application_ResolveRequestCache

2010-03-28 15:01:39 522 Application_PostResolveRequestCache

15:01:39 522 Application_PostMapRequestHandler

2010-03-28 15:01:39 522 Application_AcquireRequestState

15:01:39 537 Application_PostAcquireRequestState

2010-03-28 15:01:39 537 Application_PreRequestHandlerExecute

15:01:39 553 Application_Error

15:01:39 553 Application_EndRequest

15:01:39 569 Application_PreSendRequestHeaders

15:01:39 569 Application_PreSendRequestContent

We can see that the Application_Error event is triggered, that is, HttpRuntime considers this as an error.

Then, you can browse an existing page. If the Application is shut down due to a serious error (such as a web. config configuration error), The Calling sequence is as follows:
15:03:47 704 Application_BeginRequest

2010-03-28 15:03:47 704 Application_AuthenticateRequest

15:03:47 766 Application_PostAuthenticateRequest

15:03:47 766 Application_AuthorizeRequest

15:03:47 766 Application_PostAuthorizeRequest

2010-03-28 15:03:47 766 Application_ResolveRequestCache

2010-03-28 15:03:47 783 Application_PostResolveRequestCache

15:03:48 667 Application_PostMapRequestHandler

2010-03-28 15:03:48 667 Application_AcquireRequestState

15:03:48 683 Application_PostAcquireRequestState

2010-03-28 15:03:48 698 Application_PreRequestHandlerExecute

15:03:48 745 Page_PreInit

15:04:02 903 Page_Unload

15:04:02 903 Application_Error

15:04:02 918 Application_EndRequest

15:04:02 996 Application_PreSendRequestHeaders

15:04:02 996 Application_PreSendRequestContent

15:04:03 371 Application_Disposed

15:04:03 371 Dispose

15:04:03 386 Application_End

By comparing the previous steps, we will find that Application_Start and Init are not called again, and also confirm some of the conclusions mentioned above (Application_Start is triggered only once throughout the asp.net application lifecycle ), the last three outputs show that Application_Disposed, Dispose, and Application_End are called sequentially when the application is closed.

Then, "Review" (The web Server restarts) the normally accessed page. The order is as follows:
15:08:11 513 Application_Start

15:08:11 591 Init

15:08:11 591 Application_BeginRequest

2010-03-28 15:08:11 591 Application_AuthenticateRequest

15:08:11 591 Application_PostAuthenticateRequest

15:08:11 606 Application_AuthorizeRequest

15:08:11 606 Application_PostAuthorizeRequest

2010-03-28 15:08:11 606 Application_ResolveRequestCache

2010-03-28 15:08:11 606 Application_PostResolveRequestCache

15:08:11 622 Application_PostMapRequestHandler

15:08:11 637 Application_EndRequest

15:08:11 637 Application_PreSendRequestHeaders

15:08:11 637 Application_PreSendRequestContent

15:08:11 637 Application_BeginRequest

2010-03-28 15:08:11 637 Application_AuthenticateRequest

15:08:11 653 Application_PostAuthenticateRequest

15:08:11 653 Application_AuthorizeRequest

15:08:11 653 Application_PostAuthorizeRequest

2010-03-28 15:08:11 653 Application_ResolveRequestCache

2010-03-28 15:08:11 653 Application_PostResolveRequestCache

15:08:11 653 Application_PostMapRequestHandler

15:08:11 653 Session_Start

2010-03-28 15:08:11 653 Application_AcquireRequestState

15:08:11 653 Application_PostAcquireRequestState

2010-03-28 15:08:11 653 Application_PreRequestHandlerExecute

15:08:11 669 Page_PreInit

15:08:11 684 Page_Init

15:08:11 684 Page_InitComplete

15:08:11 684 Page_PreLoad

15:08:11 684 Page_Load

15:08:11 684 Page_LoadComplete

15:08:11 684 Page_PreRender

15:08:11 684 Page_SaveStateComplete

15:08:11 700 Page_Unload

2010-03-28 15:08:11 700 Application_PostRequestHandlerExecute

15:08:11 700 Application_ReleaseRequestState

2010-03-28 15:08:11 700 Application_PostReleaseRequestState

15:08:11 700 Application_UpdateRequestCache

2010-03-28 15:08:11 700 Application_PostUpdateRequestCache

15:08:11 700 Application_EndRequest

15:08:11 700 Application_PreSendRequestHeaders

15:08:11 700 Application_PreSendRequestContent

15:08:11 793 Application_BeginRequest

2010-03-28 15:08:11 793 Application_AuthenticateRequest

15:08:11 793 Application_PostAuthenticateRequest

15:08:11 793 Application_AuthorizeRequest

15:08:11 793 Application_PostAuthorizeRequest

2010-03-28 15:08:11 793 Application_ResolveRequestCache

2010-03-28 15:08:11 793 Application_PostResolveRequestCache

15:08:11 809 Application_PostMapRequestHandler

2010-03-28 15:08:11 809 Application_AcquireRequestState

15:08:11 809 Application_PostAcquireRequestState

2010-03-28 15:08:11 809 Application_PreRequestHandlerExecute

2010-03-28 15:08:11 825 Application_PostRequestHandlerExecute

15:08:11 825 Application_ReleaseRequestState

2010-03-28 15:08:11 840 Application_PostReleaseRequestState

15:08:11 949 Application_UpdateRequestCache

2010-03-28 15:08:11 949 Application_PostUpdateRequestCache

15:08:11 965 Application_EndRequest

15:08:11 981 Application_PreSendRequestHeaders

15:08:11 981 Application_PreSendRequestContent

Wow! When a Page is accessed, so many methods are called. No wonder many large websites with high concurrency usually have to write a reduced HttpHandler to replace the Page as the base class, to achieve better performance

Finally, when developing a website, we cannot only use the Page. In many cases, we also use UserControl (custom control). Let's take a look at its inheritance relationship, for example, we have created a user control named TestUserControl.

TestUserControl --> UserControl ---> TemplateControl --> Control

In the definition of the Control class, we can see that


This seems to indicate that the user control should contain Page_Init, Page_Load, Page_Unload... usually we only use Init and Load events. If a user control is added, the entire lifecycle will be more complex:

2010-06-12 15:35:28 042 Application_Start

2010-06-12 15:35:28 072 Init

2010-06-12 15:35:28 072 Application_BeginRequest

2010-06-12 15:35:28 082 Application_AuthenticateRequest

2010-06-12 15:35:28 082 Application_PostAuthenticateRequest

2010-06-12 15:35:28 092 Application_AuthorizeRequest

15:35:28 102 Application_PostAuthorizeRequest

2010-06-12 15:35:28 102 Application_ResolveRequestCache

2010-06-12 15:35:28 112 Application_PostResolveRequestCache

15:35:28 122 Application_PostMapRequestHandler

15:35:28 142 Application_EndRequest

15:35:28 142 Application_PreSendRequestHeaders

15:35:28 142 Application_PreSendRequestContent

15:35:28 152 Application_BeginRequest

2010-06-12 15:35:28 152 Application_AuthenticateRequest

15:35:28 162 Application_PostAuthenticateRequest

15:35:28 162 Application_AuthorizeRequest

15:35:28 162 Application_PostAuthorizeRequest

2010-06-12 15:35:28 172 Application_ResolveRequestCache

2010-06-12 15:35:28 172 Application_PostResolveRequestCache

15:35:28 172 Application_PostMapRequestHandler

15:35:28 172 Session_Start

2010-06-12 15:35:28 172 Application_AcquireRequestState

15:35:28 182 Application_PostAcquireRequestState

2010-06-12 15:35:28 182 Application_PreRequestHandlerExecute

15:35:28 192 Page_PreInit

15:35:28 192 TestUserControl. Page_Init

15:35:28 202 Page_Init

15:35:28 202 TestUserControl. TestProperty. Set

15:35:28 202 Page_InitComplete

15:35:28 202 Page_PreLoad

15:35:28 202 Page_Load

15:35:28 202 TestUserControl. Page_Load

15:35:28 202 TestUserControl. ShowData ()

15:35:28 212 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 212 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 212 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 212 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 212 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 222 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 222 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 222 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 222 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 222 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 232 TestUserControl. Repeater1.ItemDataBound ()

15:35:28 232 Page_LoadComplete

15:35:28 232 Page_PreRender

15:35:28 232 TestUserControl. Page_PreRender

15:35:28 242 Page_SaveStateComplete

15:35:28 242 TestUserControl. Page_Unload

15:35:28 252 Page_Unload

2010-06-12 15:35:28 252 Application_PostRequestHandlerExecute

15:35:28 252 Application_ReleaseRequestState

2010-06-12 15:35:28 252 Application_PostReleaseRequestState

15:35:28 262 Application_UpdateRequestCache

2010-06-12 15:35:28 262 Application_PostUpdateRequestCache

15:35:28 262 Application_EndRequest

15:35:28 272 Application_PreSendRequestHeaders

15:35:28 272 Application_PreSendRequestContent

15:35:28 282 Application_BeginRequest

2010-06-12 15:35:28 292 Application_AuthenticateRequest

15:35:28 292 Application_PostAuthenticateRequest

15:35:28 302 Application_AuthorizeRequest

15:35:28 302 Application_PostAuthorizeRequest

2010-06-12 15:35:28 302 Application_ResolveRequestCache

2010-06-12 15:35:28 312 Application_PostResolveRequestCache

15:35:28 312 Application_PostMapRequestHandler

2010-06-12 15:35:28 322 Application_AcquireRequestState

15:35:28 322 Application_PostAcquireRequestState

2010-06-12 15:35:28 322 Application_PreRequestHandlerExecute

2010-06-12 15:35:28 332 Application_PostRequestHandlerExecute

15:35:28 332 Application_ReleaseRequestState

2010-06-12 15:35:28 332 Application_PostReleaseRequestState

15:35:28 342 Application_UpdateRequestCache

2010-06-12 15:35:28 342 Application_PostUpdateRequestCache

15:35:28 342 Application_EndRequest

15:35:28 342 Application_PreSendRequestHeaders

15:35:28 342 Application_PreSendRequestContent

2010-06-12 15:36:40 034 Session_End


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.