DekGenius.com
[ Team LiB ] Previous Section Next Section

Chapter 14. The HttpContext Class

With all the knowledge gained about the HttpApplicationState class covered in the last chapter, the next question is, "How you gain access to a copy of the HttpApplicationState within your application?" The good news is that within an ASP.NET page, the Application instance of the HttpApplicationState class is available exactly as it appears in a classic ASP page. The Response, Request, and other objects familiar to classic ASP are also available. These and other objects are available by using the HttpContext class.

Unlike many classes within ASP.NET, the HttpContext class adds new methods and properties but does not contain any significant methods or properties carried over from classic ASP that are deprecated in ASP.NET. New properties include IsCustomErrorEnabled, IsDebuggingEnabled, SkipAuthorization, and Trace.

The HttpContext class encapsulates all the HTTP-specific information about a given HTTP request. The HttpContext class contains an Items collection that allows the developer to store information for the duration of the current request. In some ways, this class is similar to HttpSessionState (discussed in Chapter 19). However, information stored in the HttpContext collection is held only for the duration of the current request. While this might not initially seem useful, it is often helpful.

For instance, suppose an application is structured so that the user enters information into a form and clicks a button with a server-side event handler. When the button's Click handler is called, a different page must get the information. After placing the information gathered from the form in the HttpContext class, the click handler can use the server-side Server.Transfer method to go to the second page without requiring another round trip from the server to the client. In addition to other pages, HttpHandlers and HttpModules that might participate in a given request have access to the context.

The HttpContext object will seem a bit redundant for developers who only write traditional ASP.NET pages. Most of the properties and methods are duplicated in the Page object, so they might not seem important. However, if you are creating other types of ASP.NET code, such as HttpModules and HttpHandlers, the HttpContext class can be a lifesaver. Even developers creating only standard ASP.NET pages might need to use the HttpContext object if they are creating event handlers in the global.asax file. In some contexts, the traditional objects used within ASP.NET pages are not available within these global.asax event handlers.

Table 14-1 lists the properties, collections, and methods exposed by the HttpContext class.

Table 14-1. HttpContext class summary

Properties

Collections

Methods (instance)

Methods (static/shared)

Application

AllErrors

AddError

GetAppConfig

ApplicationInstance

Items

ClearError

 

Cache

 

GetConfig

 

Current

 

RewritePath

 

Error

   

Handler

   

IsCustomErrorEnabled

   

IsDebuggingEnabled

   

Request

   

Response

   

Server

   

Session

   

SkipAuthorization

   

Timestamp

   

Trace

   

User

   

    [ Team LiB ] Previous Section Next Section