In this blog I am trying to explain the concept of "Global.asax" in asp.net.
The Global.asax file is an optional file that is located in the application's root directory and is the ASP.NET counterpart of the Global.asa of ASP. This file exposes the application and session level events in ASP.NET and provides a gateway to all the application and the session level events in ASP.NET. This file can be used to implement the important application and session level events such as Application_Start, Application_End, Session_Start, Session_End, etc. This article provides an overview of the Global.asax file, the events stored in this file and how we can perform application wide tasks with the help of this file.
The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.
Types of Events in Global.asax
The Global.asax contains two types of events those are
1. Events which are fired for every request
2. Events which are not fired for every request
Methods corresponding to events that fire on each request
1. Application_BeginRequest() – This event fired when a request for the web application comes in.
2. Application_AuthenticateRequest – This event fired just before the user credentials are authenticated. You can specify your own authentication logic over here.
3. Application_AuthorizeRequest() – This event fired on successful authentication of user’s credentials. You can use this method to give authorization rights to user.
4. Application_ResolveRequestCache() – This event fired on successful completion of an authorization request.
5. Application_AcquireRequestState() – This event fired just before the session state is retrieved for the current request.
6. Application_PreRequestHandlerExecute() – This event fired before the page framework begins before executing an event handler to handle the request.
7. Application_PostRequestHandlerExecute() – This event fired after HTTP handler has executed the request.
8. Application_ReleaseRequestState() – This event fired before current state data kept in the session collection is serialized.
9. Application_UpdateRequestCache()This event fired before information is added to output cache of the page.
10. Application_EndRequest() – This event fired at the end of each request
Methods corresponding to events that do not fire on each request
1. Application_Start() – This event fired when the first resource is requested from the web server and the web application starts.
2. Session_Start() – This event fired when session starts on each new user requesting a page.
3. Application_Error() – This event fired when an error occurs.
4. Session_End() – This event fired when the session of a user ends.
5. Application_End() – This event fired when the web application ends.
6. Application_Disposed() - This event fired when the web application is destroyed.
Adding Global.asax file:Go to Website -> Add New Item and find the icon that says Global Application Class. Add this item, and then insert the code into its text. If you are using a web application project, use the code-behind file.