The Global.asax File in ASP.NET Websites
Hopefully, you reached this page from the About Visual Basic ASP.NET Hub Page. If not, you might want to click the link above and go there.
If your website needs to respond to an event for the entire application or session, you can put the code to do it in a special file named Global.asax. This file isn't a default part of ASP.NET websites. You have to add it using Add New ... in Solution Explorer (and it has to be in the virtual root of the website).
The illustration below shows the file being added.
--------
Click Here to display the illustration
--------
Keep in mind that this isn't a general code module. Methods in Global.asax will only run in response to specific events. In particular, the events are for the System.Web.HttpApplication class. This is the class that defines the methods, properties, and events that are common to all application objects in an ASP.NET application. Because there's a clear security risk, IIS rejects any direct request (using a URL from a browser) for Global.asax.
Understanding the events that Global.asax will respond to is the key to using it. These are the events in the order that they're fired:
One of the features of IIS is the ability to change applications without restarting the server and Global.asax is a good example. When Global.asax is changed in a running server, ASP.NET detects the that the file has been changed and reboots the application. All browser sessions are closed and state is restarted. (See Using State in ASP.NET Web Pages for an explanation.)
If your website needs to respond to an event for the entire application or session, you can put the code to do it in a special file named Global.asax. This file isn't a default part of ASP.NET websites. You have to add it using Add New ... in Solution Explorer (and it has to be in the virtual root of the website).
The illustration below shows the file being added.
--------
Click Here to display the illustration
--------
Keep in mind that this isn't a general code module. Methods in Global.asax will only run in response to specific events. In particular, the events are for the System.Web.HttpApplication class. This is the class that defines the methods, properties, and events that are common to all application objects in an ASP.NET application. Because there's a clear security risk, IIS rejects any direct request (using a URL from a browser) for Global.asax.
Understanding the events that Global.asax will respond to is the key to using it. These are the events in the order that they're fired:
- Application_BeginRequest
- Application_AuthenticateRequest
- Application_AuthorizeRequest
- Application_ResolveRequestCache
- Application_AcquireRequestState
- Application_PreRequestHandlerExecute
- Application_PreSendRequestHeaders
- Application_PreSendRequestContent
- <<code is executed>>
- Application_PostRequestHandlerExecute
- Application_ReleaseRequestState
- Application_UpdateRequestCache
- Application_EndRequest
One of the features of IIS is the ability to change applications without restarting the server and Global.asax is a good example. When Global.asax is changed in a running server, ASP.NET detects the that the file has been changed and reboots the application. All browser sessions are closed and state is restarted. (See Using State in ASP.NET Web Pages for an explanation.)