Mvc. global. asax event, mvcglobal. asax
1. 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.
2. 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
3. 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.
Public class MvcApplication: System. web. httpApplication {protected void Application_Start (object sender, EventArgs e) {// not every request is called // It is executed once in the life cycle of the Web application // It is called when the application is started for the first time and when the application domain is created // suitable for processing application Scope initialization code} void Application_End (object sender, eventArgs e) {// it is not called every request. // the code that runs when the application is closed. Run the code after the last HttpApplication is destroyed. // For example, IIS restart, file update, process recycling causes the application to be converted to another application domain} void Session_Start (object sender, EventArgs e) {// not for each All requests are called // executed at session start} void Session_End (object sender, EventArgs e) {// not every request is called // executed when the Session ends or expires // no matter whether the Session is explicitly cleared or the Session expires automatically in the code, this method will be called} void Application_Init (object sender, EventArgs e) {// not every request is called // executed during initialization of each HttpApplication instance} void Application_Disposed (object sender, eventArgs e) {// not every request is called // after the application is closed for a period of time. the. net garbage collector is called when it is ready to recycle the memory it occupies. // Execute void Application_Error (object sender, EventArgs e) before each HttpApplication instance is destroyed) {// not every request is called // 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. The first execution of this method} void Application_Authentic AteRequest (object sender, EventArgs e) {// occurs before verification is executed. This is the start 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 authorization event to enable the cache module to provide services for requests from the cache, thus skipping the execution of the Processing Program (page or WebService. // 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, the web form will be compiled and instantiated // -------------------------------------------------------------------------- void Application_AcquireRequestState (object sender, EventArgs e) {// read the specific information required by the Session and run it before filling the information into the Session} void Application_PreRequestHandlerExecute (object sender, EventArgs e) {// call before the appropriate processing program executes the request // this time, the Session can be used} // ------------------------------------------------------- // at this time, the page code will be executed, page rendered as HTML //-- --------------------------------------------- Void Application_PostRequestHandlerExecute (object sender, EventArgs e) {// It is called when the handler completes processing the request.} Void Application_ReleaseRequestState (object sender, EventArgs e) {// Release Request status} void Application_UpdateRequestCache (object sender, EventArgs e) {// for subsequent requests, void Application_EndRequest (object sender, EventArgs e) {// EndRequest is the last event triggered when the Request is returned // However, it is suitable to clear the code before the object is released or re-established.} void Application_PreSendRequestHeaders (object sender, eventArgs e) {// called before sending the Http header to the client} void Application_PreSendRequestContent (object sender, EventArgs e) {// called before sending the Http body to the client }}