Hello everyone, in this
post I will explore the Global.asax file used for handling application level
events in ASP.NET
In ASP.NET
applications, events can occur at three levels
1)Application
Level Eg : application start event, application end event, session start event
etc
2)Page
Level Eg : PageLoad, PageRerender
3)Control
Level Eg : Button click, Drop down
change event etc
Definition
(as per Microsoft) - The Global.asax file, also known as the ASP.NET
application file, is an optional file that contains code for responding
application level events raised by ASP.NET or by HttpModules.
The
following points are to be remembered about Global.asax file
1) It is
automatically generated in the root directory when a new web application is
created.
2) Global class
is derived from the class System.Web.HttpApplication.
3) Global.asax
file is not accessible from outside. (means any direct request from the browser
for the display of the file is rejected)
4) Global.asax
file is optional and can be deleted if not required.
5) There can be
only one Global.asax file per application
6) In ASP,
Global.asax exists as Global.asa
There are
different application level events that are predefined and are available for
use in the Global class, some of them are
1) Application_Start
event
This event
is fired when the application is started and when the resource is requested for
the first time from the webserver.
2) Application_Init event
The
Application_Init event is fired when an application initializes the first time.
3) Session_Start
event
This event
is fired when a new user requests a resource for the first time from the web
server.
ie when a
new unique id is created for a new user.
Browser will
have a unique session id and this is used to by the server to know the no of
users connected.
If we open
the application in the same browser then the same cookie is sent to the server.
If we open the application in a different browser (firefox) then a new cookie is sent.
4) Application_Error
event
This event
is fired whenever there is an unhanded exception,
generally we
can use this event handler for logging the error details into the database.
5) Session_End
event
This event
is fired when a session of an existing user ends.
6) Application_End event
This event
is fired when there are no more active sessions ie after last log off.
7) Application_Disposed event
This event
is fired when the web application is destroyed ie when the application is
stopped in the IIS Server.
Uses of
Global.asax
1) Can be
used for logging during events like a new user is logged in or when a user logs
out.
2) Can be
used to track the application start and stop.
3) To Track
the no of users
4) To
release all the resource.
Further Reading
Global.asax File MSDN Link
https://msdn.microsoft.com/en-us/library/1xaas8a2%28v=vs.71%29.aspx
Global.asax File MSDN Link
https://msdn.microsoft.com/en-us/library/1xaas8a2%28v=vs.71%29.aspx