O'Reilly logo

Programming ASP.NET 3.5, 4th Edition by Jesse Liberty, Dan Hurwitz, Dan Maharry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 HttpApplication object

  • Using 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 Application object for using application state

  • The Request object for getting access to the incoming request

  • The Response object for sending an HttpResponse back to the client

  • The Session object for access to session state

ASP.NET maintains a pool of HttpApplication instances during the lifetime ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required