Overview of the global. asax File
The global. asax file contains the event handler for global application events. It responds to application-level and session-Level Event code.
At runtime, global. asax will be compiled into a dynamically generated. NET Framework class, which is derived from the httpapplication base class.
Therefore, the code in global. asax can access all public or protected members in the httpapplication class.
Global. asax is not directly requested by users, but the code in global. asax is automatically executed to respond to specific application events.
Global. asax is optional and unique in a web project. It should be in the root directory of the website.
Complete processing of a request
The following process is executed by Internet Information service(inetinfo.exe) (IIS)
1. The client sends a request
2. Verify the request
3. Authorize the request
4. Determine the request Cache
5. Obtain the cache status
6. Before the request processing program is executed
7. Http processing program execution request (asp.netpage is executed by aspnet_wp.exe)
8. After the request processing program is executed
9. Release Request status
10. Update request Cache
11. Request ended
Events in global. asax
All events in global. asax can be divided into two types: one is triggered only when a specific event is met, and the other is events that are executed in sequence each request.
The following is an example that contains all events contained in global. asax.
Code <% @ Application language = "C #" %>
<SCRIPT runat = "server">
/*************************************** ******************************/
// The following events will not be called during each request, but will be called only when a specific event occurs.
/*************************************** ******************************/
Protected void application_start (Object sender, eventargs E)
{
// Not every request
// Execute the task once in the lifecycle of the Web application.
// Called when the application is started for the first time and the application domain is created
// Initialization code suitable for processing the application scope
}
Void application_end (Object sender, eventargs E)
{
// Not every request
// The code that runs when the application is closed. It is executed after the last httpapplication is destroyed.
// For example, IIS restart, file update, and process recycle cause the application to be switched to another application domain
}
Void session_start (Object sender, eventargs E)
{
// Not every request
// Execute when the session starts
}
Void session_end (Object sender, eventargs E)
{
// Not every request
// Run when the session ends or expires
// This method is called no matter whether the session is explicitly cleared or the session expires automatically in the code.
}
Void application_init (Object sender, eventargs E)
{
// Not every request
// Executed during initialization of each httpapplication instance
}
Void application_disposed (Object sender, eventargs E)
{
// Not every request
// It is called when the. Net garbage collector is ready to recycle the memory occupied by the application after it is disabled for a period of time.
// Executed before each httpapplication instance is destroyed
}
Void application_error (Object sender, eventargs E)
{
// Not every request
// All errors that are not processed will result in execution of this method
}
/*************************************** ******************************/
// The following events are executed in sequence for each request.
/*************************************** ******************************/
Void application_beginrequest (Object sender, eventargs E)
{
// The first departure event for each request. This method is first executed.
}
Void application_authenticaterequest (Object sender, eventargs E)
{
// Occurs before verification is executed. This is the starting point for creating the verification logic.
}
Void application_authorizerequest (Object sender, eventargs E)
{
// When the security module has verified the authorization of the current user
}
Void application_resolverequestcache (Object sender, eventargs E)
{
// When ASP. NET completes the authorization event so that the cache module can provide 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.
}
//------------------------------------------------------------------------
// At this time, the request will be forwarded to the appropriate program. For example, a web form is compiled and instantiated.
//------------------------------------------------------------------------
Void application_acquirerequeststate (Object sender, eventargs E)
{
// Read the specific information required by the session and run the command before filling the information into the session.
}
Void application_prerequesthandlerexecute (Object sender, eventargs E)
{
// Called before the appropriate handler executes the request
// The session can be used at this time.
}
//-------------------------------------------------
// At this time, the page code will be executed and displayed as HTML
//-------------------------------------------------
Void application_postrequesthandlerexecute (Object sender, eventargs E)
{
// Called after the handler completes processing the request.
}
Void application_releaserequeststate (Object sender, eventargs E)
{
// Release Request status
}
Void application_updaterequestcache (Object sender, eventargs E)
{
// Called when the response cache is updated for subsequent requests
}
Void application_endrequest (Object sender, eventargs E)
{
// Endrequest is the last event triggered in response to the request.
// But before the object is released or re-created, it is suitable to clear the code at this time.
}
Void application_presendrequestheaders (Object sender, eventargs E)
{
// Called before sending an HTTP header to the client
}
Void application_presendrequestcontent (Object sender, eventargs E)
{
// Called before sending the HTTP body to the client
}
</SCRIPT>
Clear Asp.net series learning blog directory
Reference: Pro ASP. NET 3.5 in C #2008