Introductions to “Global.asax” in Asp.Net
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.
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
Types of Events in Global.asax
Global.asax contains two types of events those are
are fired for every request
are not fired for every request
Methods corresponding to events that fire on each request
Application_BeginRequest() – This event fired when a request for the web application
Application_AuthenticateRequest – This event fired just before the user
credentials are authenticated. You can specify your own authentication logic
Application_AuthorizeRequest() – This event fired
on successful authentication of user’s credentials. You can use this method to
give authorization rights to user.
Application_ResolveRequestCache() – This event fired on successful completion of an
Application_AcquireRequestState() – This
event fired just before the session state is retrieved for the current request.
Application_PreRequestHandlerExecute() – This event fired before the page framework begins before
executing an event handler to handle the request.
Application_PostRequestHandlerExecute() – This event
fired after HTTP handler has executed the request.
Application_ReleaseRequestState() – This
event fired before current state data
kept in the session collection is serialized.
Application_UpdateRequestCache()This event fired before information is added
to output cache of the page.
Application_EndRequest() – This
event fired at the end of each request
Methods corresponding to events that do not fire on each request
Application_Start() – This
event fired when the first resource is
requested from the web server and the web application starts.
Session_Start() – This
event fired when session starts on each
new user requesting a page.
Application_Error() – This event fired when an error occurs.
Session_End() – This
event fired when the session of a user
Application_End() – This
event fired when the web application ends.
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