Application-Wide Logic
All code contained in the Page class is scoped to the page. In other words, it is visible only to other code within that Page class. This is true for variables and members such as methods, properties, and events. For most code, this is the appropriate behavior. However, often it is either convenient or necessary to have code scoped more globally. For example, you may have a common method used in several pages. Though you can replicate the code on all the pages, it is far better to have a single source. Another example would be an application where a variable—a TrackingID, for example—is needed by every page as the user moves from page to page.
It is possible to scope your code application-wide (rather than per page). There are several ways of doing this:
Using the
HttpApplicationobjectUsing the Global.asax file
Using HTTP handlers and modules
The HttpApplication Object
Just as a web page instantiates the Page class, when an application runs it instantiates an object from the HttpApplication class. This object has methods, properties, and events available to all the objects within the application. It provides several objects that allow you to interact with the HTTP request:
The
Applicationobject for using application stateThe
Requestobject for getting access to the incoming requestThe
Responseobject for sending anHttpResponseback to the clientThe
Sessionobject for access to session state
ASP.NET maintains a pool of HttpApplication instances during the lifetime ...