Global. asax File

Source: Internet
Author: User
Tags website performance

From: http://hi.baidu.com/mycolorwind/blog/item/45384980228cbfdf9023d960.html

I found n many related things on the Internet, but I have finally found the relationship between web. cofig and Global. asax and the execution sequence.

In the global. asax. CS File

Protected void application_beginrequest (Object sender, eventargs E)
{
Application ["starttime"] = system. datetime. now;
}

Add
Private void page_load (Object sender, system. eventargs E)
{
System. datetime starttime = (system. datetime) application ["starttime"];
System. datetime endtime = system. datetime. now;
System. timespan Ts = endtime-starttime;
Response. Write ("Page execution time:" + ts. milliseconds + "millisecond ");
}

If it is in version 05, you must manually add it. Add global in app_code. asax. CS, and then set global. asax attribute inherits = "Global" or codebehind = "Global. asax. CS ": <% @ application codebehind =" Global. asax. CS "%>

All tests above passed 03/05.

The following describes how to understand the global. asax file:

1. Role of the global. asax File

Let's take a look at the msdn explanation. The Global. asax file (also known as an ASP. NET application)ProgramFile) is an optional file that contains the application-level and session-level events caused by the ASP. NET or HTTP module.Code. The global. asax file resides in the root directory of the ASP. NET application. At runtime, analyze global. asax and compile it into a dynamically generated. NET Framework class, which is derived from the base class of httpapplication. Configure ASP. NET to automatically reject any direct URL requests to the global. asax file. external users cannot download or view the code. The global. asax file is optional. You must create an application event or session event only when you want to process it.

2 create a global. asax File

Click website> Add new item> global application class to add the global. asax file. In. in net2003, right-click global. view the code in the asax file to edit global. asax. CS file, but in. net2005 does not have this option, so you need to add global in app_code. asax. CS, and then set global. asax attribute inherits = "Global" or codebehind = "Global. asax. CS. I don't know if there are any better methods.

3Execution of the global. asax File

Worker Process (w3wp.exe process in Windows Server 2003). This process passes the request to a specified appdomain. When this appdomain is created, some information in the configuration file will be loaded (the Loading Order is from the machine. config file to web. when all the information is loaded, the appdomain will obtain an httpapplication instance. At this time, the global class will be compiled and loaded, next, appdomain will perform some related processing to create a page class instance, and the page is finally displayed on the client browser. Note that when the configuration file is loaded, the appdomain does not load all the information in the configuration file, but only some required information. Some configuration information is loaded by the appdomain only when necessary. For example, we have configured many httpmodules in the web. config file, but the appdomain will load and process the information only when each httpmodule is accessed. Therefore, the Web. config file and global file are not executed sequentially, but they only depend on when the specific information will be loaded and processed.

4. Meanings of methods in global. asax. CS

Application_init: executed during the initialization of each httpapplication instance

Application_disposed: executed before each httpapplication instance is destroyed

Application_error: all errors that are not processed will result in execution of this method.

Application_start: executed during program initialization. It is executed once in the lifecycle of a Web application. Only public information such as httpapplicationstate can be put here.

Application_end: when the application ends, it is executed after the last httpapplication is destroyed. It corresponds to application_start and is only executed once throughout the lifecycle.

Session_start: run when the session starts.

Session_end: run when the session ends or expires.

Application_beginrequest: beginrequest is the first event triggered when a request is received. This method is first executed.

Application_authenticaterequest: run the command after the security module has set up the identity of the current user.

Application_authorizerequest: executed when the security module has verified the authorization of the current user.

Application_resolverequestcache: When ASP. NET completes the authorization event so that the cache module provides services for requests from the cache, the execution of the Processing Program (page or WebService) is skipped. This can improve the website performance. This event can also be used to determine whether the body is obtained from the cache.

Application_acquirerequeststate: executed when ASP. NET obtains the current State (such as session) associated with the current request.

Application_prerequesthandlerexecute: executed before ASP. NET sends the request to the processing program object (page or WebService. At this time, the session can be used.

Application_postrequesthandlerexecute: it is executed after the processing program object is finished.

Application_releaserequeststate: it is executed after ASP. NET executes all request processing programs. The releaserequeststate event saves the current state data.

Application_updaterequestcache: it is executed after ASP. NET executes the processing program and updates the response cache for subsequent requests.

Application_endrequest: Same as above. endrequest is the last event triggered in the response to the request. This method is naturally the last one to be executed.

Application_presendrequestheaders: executed before the HTTP header is sent to the client.

Application_presendrequestcontent: executed before the HTTP body is sent to the client.

The order of request event execution:

1. beginrequest

2. authenticaterequest

3. authorizerequest

4. resolverequestcache

5. acquirerequeststate

6. prerequesthandlerexecute

7. postrequesthandlerexecute

8. releaserequeststate

9. updaterequestcache

10. endrequest

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.