[transfer] Global.asax file in ASP.NET MVC

1. Overview of global.asax file

The global.asax file contains event handlers for global application events. Code that responds to application level and session level events.   
At runtime, Global.asax will be compiled into a dynamically generated. NET Framework class derived from the HttpApplication base class.
So the code in global.asax can access all the public or protected members of HttpApplication class
Global.asax is not directly requested by users, but the code in global.asax is automatically executed in response to specific application events.
global.asax is optional and unique in a web project, and should be at the root of the site.

2. Complete processing of a request

The following procedure is performed by Internet Information Service (inetinfo.exe) (IIS)
1. Request from client
2. Verification request
3. Authorization of requests
4. Determine the requested cache
5. Get cache status
6. Before executing the requested handler
7.http handler execution request (asp.net page is executed by aspnet? WP. Exe)
8. After the requested handler is executed
9. Release request status
10. Update request cache
11. End of request

Events in 3.global.asax

All events in global.asax can be divided into two types: one is triggered when a specific event is satisfied, and the other is the event that each request will be executed in sequence.

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            //Not every request
            //Once in the life cycle of a Web application
            //When the application is first started and the application domain creation is called
            //Initialization code suitable for handling application scope
        }

        void Application_End(object sender, EventArgs e)
        {
            //Not every request
            //Code that runs when the application closes, executed after the last HttpApplication is destroyed
            //For example, IIS restart, file update, and process recycling lead to application conversion to another application domain
        }

        void Session_Start(object sender, EventArgs e)
        {
            //Not every request
            //Execute at the beginning of the session
        }

        void Session_End(object sender, EventArgs e)
        {
            //Not every request
            //Execute when session ends or expires
            //This method will be called no matter whether the Session is cleared explicitly in the code or the Session timeout expires automatically
        }

        void Application_Init(object sender, EventArgs e)
        {
            //Not every request
            //Execute when each HttpApplication instance is initialized
        }

        void Application_Disposed(object sender, EventArgs e)
        {
            //Not every request
            //After the application has been shut down for some time, it is called when the. net garbage collector is ready to reclaim the memory it uses.
            //Execute before every HttpApplication instance is destroyed
        }

        void Application_Error(object sender, EventArgs e)
        {
            //Not every request
            //All unhandled errors result in the execution of this method
        }


        /*********************************************************************/
        //Each request performs the following events in sequence
        /*********************************************************************/

        void Application_BeginRequest(object sender, EventArgs e)
        {
            //The first event of departure at each request, the first execution of this method
        }

        void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            //Occurs before validation is performed, which is the starting point for creating validation logic
        }

        void Application_AuthorizeRequest(object sender, EventArgs e)
        {
            //Executed when the security module has verified the authorization of the current user
        }

        void Application_ResolveRequestCache(object sender, EventArgs e)
        {
            //Occurs when ASP.NET completes the authorization event to cause the cache module to service the request from the cache, skipping the execution of the handler (page or WebService).
            //This can improve the performance of the website. This event can also be used to determine whether the text is obtained from the Cache.
        }

        //------------------------------------------------------------------------
        //At this time, the request will be forwarded to the appropriate process. For example: web forms will be compiled and instantiated
        //------------------------------------------------------------------------

        void Application_AcquireRequestState(object sender, EventArgs e)
        {
            //Read the specific information required by the Session and execute it before filling it into the Session
        }

        void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            //Call before the appropriate processing program executes the request.
            //At this time, Session can be used
        }

        //-------------------------------------------------
        //At this time, the page code will be executed and the page will be rendered as HTML
        //-------------------------------------------------

        void Application_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            //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)
        {
            //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 a Request
            //But before the object is released or newly created, it is appropriate to clean up the code at this time
        }

        void Application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            //Called before sending Http header to client
        }

        void Application_PreSendRequestContent(object sender, EventArgs e)
        {
            //Called before sending Http body to client
        }
    }

Tags: Session IIS

Posted on Thu, 02 Jan 2020 11:43:19 -0500 by t2birkey