ASP.net application objects and page life cycle _ Practical Tips

Source: Internet
Author: User
Tags httpcontext

When IIS receives a new HTTP request, it eventually invokes the Asp.net_isapi.dll ISAPI extension (especially the IIS6.0 environment, where the iis7.0 application Chime considers the integration method to be relatively variable) and then passes to httpruntime Pipe ( HTTP runtime pipeline), ASP.net then began to run (ie httpruntime is the asp.net real portal), httpruntime will be for every An asp.net application automatically creates an instance of a HttpApplication, and the instance contains the following properties:

Note 1

Application--> is equivalent to the traditional application object in the age of ASP, which is usually used to define a global variable for asp.net application.

Instance of the context-->httpcontext (contextual) class "ASP.net new"

Modules--> A collection of HttpModule modules that affect the current application

The request--> is similar to the Request object in ASP, and is typically used to receive certain values (such as Request.Form or request.querystring)

Response--> similar to Response objects in ASP, typically used to specify content for page output (such as Resonse.write)

The server--> is similar to the server object in ASP, by which you can obtain information about the service side (such as Server.MapPath)

Session--> similar to the Session object in ASP

User--> is used to obtain the security information associated with the authentication


From the above properties can be found: many in fact in the ASP era has been used, only context,modules,user these three are asp.net new


In addition to having several attributes of "Note 1", The HttpApplication class has its own method, which is specifically referred to as the Init method and the Dispose method, which can be overloaded with two methods.

The timing of their invocation is:

The Init method is called after Application_Start, and Dispose is invoked before Application_End, and Application_Start is fired only once in the life cycle of the entire ASP.net application ( such as IIS startup or Web site startup, a similar application_end is invoked only when the ASP.net application shuts down (for example, when IIS stops or the site stops)

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

These events include the overloaded Init and Dispose methods mentioned earlier, plus the Session_Start and Session_End methods corresponding to the session, which can be directly application_ in Global.ascx.cs XXX is used in the form of (because the class global defined in Global.ascx.cs itself is inherited from HttpApplication)

Copy Code code as follows:

public class Global:System.Web.HttpApplication

Let's take a look at the three properties of the new Context,modules,user relative to ASP

Context:

The context is an instance of the HttpContext class, which has been passed down throughout the entire ASPX page lifecycle with each link

So we can almost any part of the Web application, using HttpContext.Current to refer to the current context instance, from the definition of HttpContext, you can also find the properties of the contexts themselves, and can get application, Applicationinstance,profile,response.request ... Instance references to objects such as

Look Back:

Copy Code code 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 a ashx file, the ProcessRequest method is to pass the current context in, and then get a reference to the response object through the contexts, and eventually we can output any desired content to the page.

Modules:

Each class that implements the IHttpModule interface can be considered an HTTP module component that can be interpreted as an HTTP request interceptor, which, when intercepted to an HTTP request, can modify the context in which it is being processed, and then return control to the pipeline if there are other modules , then continue processing until all HttpModule in the Modules collection are "cool" (note: Poor HTTP requests give each HttpModule round x)

asp.net2.0 a number of HttpModule are built into the default, and the following default built-in modules can be found from the Machine.config file:

NOTE 2
Anonymouseidentification--Assigning a temporary identity to an anonymous user
Fileauthorization--verifies that the user has a Windows NT license to request files
FormsAuthentication--form authentication Module (if this module is not available, asp.net cannot be validated as user name/password [i.e. forms])
OutputCache--Output cache module
Passportauthentication--passport Verification Module
Profile--User Configuration module (without it, asp.net can not use profiles)
RoleManager--Role management
Sessionsate--Session state module
URLAuthorization--A URL based authentication module
WindowsAuthentication--windows and IIS authentication modules

User:

If you use the asp.net2.0 built-in membership/role mechanism for access authentication, you will be familiar with the user object, such as:

Copy Code code as follows:

if (HttpContext.Current.User.Identity.IsAuthenticated)
{
User logged in ...
}


We often use it to determine the login status of the current browsing user, and for a more detailed definition of the user class, see MSDN


Life cycle:

Finally, take a look at the life cycle of page in ASP.net, where several events are defined in page:

Overall: When an ASPX page is requested, the final lifecycle is triggered or invoked by the above event defined in page (and some overloaded callback methods) and by the events defined in the previously mentioned HttpApplication class (with the corresponding callback method). A series of processes that are eventually superimposed.

If you do not consider the event handling method in HttpApplication (that is, regardless of the application_xxx processing we defined in Global.ascx.cs), the event (method) in the page is normally triggered (called) in the order:

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 in which the page page is not postback, and without regard to the face control, the order changes slightly if you add a page postback (such as placing a Asp:button in the page and then adding a handler function to the Button's click Postback event):

01.page_preinit

02.page_init

03.page_initcomplete

04.page_preload

05.page_load

06.button1_click

07.page_loadcomplete

08.page_prerender

09.page_savestatecomplete

10.page_unload

The difference is that the postback event button1_click is triggered after Page_Load.

Finally take the HttpApplication event into account, look at the stacking order, but don't worry, we first look at a special situation, if a asp.net application root directory does not set the default page, then directly browse the root directory, such as http://localhost : 2345/When the Application_xxx method defined in Globl.ascx.cs is called in the following order:
2010-03-28 15:01:39 413 Application_Start

2010-03-28 15:01:39 491 Init

2010-03-28 15:01:39 491 Application_BeginRequest

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

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

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

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

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

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

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

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

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

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

2010-03-28 15:01:39 553 Application_Error

2010-03-28 15:01:39 553 Application_EndRequest

2010-03-28 15:01:39 569 Application_presendrequestheaders

2010-03-28 15:01:39 569 application_presendrequestcontent

You can see that the Application_Error event is triggered, that is, HttpRuntime thinks it is an error.

Then browse a real-world page, and if the application has a serious error that causes application to shut down (such as a Web.config configuration error), the order of the calls is as follows:
2010-03-28 15:03:47 704 Application_BeginRequest

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

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

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

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

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

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

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

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

2010-03-28 15:03:48 683 Application_postacquirerequeststate

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

2010-03-28 15:03:48 745 Page_PreInit

2010-03-28 15:04:02 903 Page_Unload

2010-03-28 15:04:02 903 Application_Error

2010-03-28 15:04:02 918 Application_EndRequest

2010-03-28 15:04:02 996 Application_presendrequestheaders

2010-03-28 15:04:02 996 Application_presendrequestcontent

2010-03-28 15:04:03 371 application_disposed

2010-03-28 15:04:03 371 Dispose

2010-03-28 15:04:03 386 Application_End

In contrast to the order, it will be found that Application_Start and Init have not been invoked again, and that some of the conclusions mentioned earlier in the article (Application_Start are triggered only once throughout the ASP.net application lifecycle) And the last three outputs are known: Application_disposed,dispose,application_end are invoked sequentially when the application shuts down.

Again "re" browsing (meaning Web server restarts) The normal access page, in the case of no error or postback, the order is as follows:
2010-03-28 15:08:11 513 Application_Start

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

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

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

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

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

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

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

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

2010-03-28 15:08:11 622 Application_postmaprequesthandler

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2010-03-28 15:08:11 669 Page_PreInit

2010-03-28 15:08:11 684 Page_Init

2010-03-28 15:08:11 684 Page_initcomplete

2010-03-28 15:08:11 684 Page_preload

2010-03-28 15:08:11 684 Page_Load

2010-03-28 15:08:11 684 Page_loadcomplete

2010-03-28 15:08:11 684 Page_PreRender

2010-03-28 15:08:11 684 Page_savestatecomplete

2010-03-28 15:08:11 Page_Unload

2010-03-28 15:08:11 Application_postrequesthandlerexecute

2010-03-28 15:08:11 Application_releaserequeststate

2010-03-28 15:08:11 Application_postreleaserequeststate

2010-03-28 15:08:11 Application_updaterequestcache

2010-03-28 15:08:11 Application_postupdaterequestcache

2010-03-28 15:08:11 Application_EndRequest

2010-03-28 15:08:11 Application_presendrequestheaders

2010-03-28 15:08:11 application_presendrequestcontent

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2010-03-28 15:08:11 965 Application_EndRequest

2010-03-28 15:08:11 981 Application_presendrequestheaders

2010-03-28 15:08:11 981 Application_presendrequestcontent

Wow! Originally a page access down, will call so many methods, no wonder many high concurrent large sites, usually have to write their own a refined httphandler used to replace page as the base class, in order to expect better performance

Finally: We do web development, it is not possible to use only page page, many times will also use UserControl (user-defined control), first look at its inheritance, such as we created a Testusercontrol user control

Testusercontrol--> UserControl---> TemplateControl--> control

Finally, under the definition of the control class, you can see

This seems to indicate that there should be page_init,page_load,page_unload in the user control ... And so on, usually we only use the Init,load event, and if you add a user control, the entire lifecycle is 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

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

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

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

2010-06-12 15:35:28 122 Application_postmaprequesthandler

2010-06-12 15:35:28 Application_EndRequest

2010-06-12 15:35:28 Application_presendrequestheaders

2010-06-12 15:35:28 application_presendrequestcontent

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

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

2010-06-12 15:35:28 162 Application_postauthenticaterequest

2010-06-12 15:35:28 162 Application_authorizerequest

2010-06-12 15:35:28 162 Application_postauthorizerequest

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

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

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

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

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

2010-06-12 15:35:28 Application_postacquirerequeststate

2010-06-12 15:35:28 Application_prerequesthandlerexecute

2010-06-12 15:35:28 Page_PreInit

2010-06-12 15:35:28 Testusercontrol.page_init

2010-06-12 15:35:28 Page_Init

2010-06-12 15:35:28 TestUserControl.TestProperty.Set

2010-06-12 15:35:28 Page_initcomplete

2010-06-12 15:35:28 Page_preload

2010-06-12 15:35:28 Page_Load

2010-06-12 15:35:28 Testusercontrol.page_load

2010-06-12 15:35:28 Testusercontrol.showdata ()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 212 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 222 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 232 TestUserControl.Repeater1.ItemDataBound ()

2010-06-12 15:35:28 232 Page_loadcomplete

2010-06-12 15:35:28 232 Page_PreRender

2010-06-12 15:35:28 232 Testusercontrol.page_prerender

2010-06-12 15:35:28 242 Page_savestatecomplete

2010-06-12 15:35:28 242 Testusercontrol.page_unload

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

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

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

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

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

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

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

2010-06-12 15:35:28 272 application_presendrequestheaders

2010-06-12 15:35:28 272 application_presendrequestcontent

2010-06-12 15:35:28 282 application_beginrequest

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2010-06-12 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.