Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [335]

By Root 5655 0
C#. Add the following code to a C#-based page (or code-behind class) for which you need to access the globals:

using Globals = ASP.global_asax;

The preceding statement creates an alias for the ASP.global_asax class (or whatever name your global.asax class has). The alias—Globals in this sample code—can be used throughout your code wherever ASP.global_asax is accepted. In ASP.NET 4, however, you can also rely on the dynamic type.

The HttpContext Class


During the various steps of the request’s chain of execution, an object gets passed along from class to class—this object is the HttpContext object. HttpContext encapsulates all the information available about an individual HTTP request that ASP.NET is going to handle. The HttpContext class is instantiated by the HttpRuntime object while the request processing mechanism is being set up. Next, the object is flowed throughout the various stages of the request’s lifetime.

Important

Before I get into the details of HttpContext and other ASP.NET intrinsic objects, I should note that in ASP.NET 4 all these objects inherit from a base class. For example, HttpContext derives from HttpContextBase and HttpResponse extends the capabilities of HttpResponseBase. The reason is to make it easier to write unit tests to check the behavior of code-behind classes. By using base classes, you can more easily create mocks of intrinsic objects and inject them into the classes. In Chapter 15, you saw an approach to testability that will benefit from base classes for intrinsic objects. Note that the ASP.NET Cache is not included in the list of objects with a base class.

Properties of the HttpContext Class


Table 16-6 enumerates all the properties exposed by the HttpContext class. The class represents a single entry point for a number of intrinsic objects such as classic ASP intrinsics and ASP.NET-specific Cache and User objects.

Table 16-6. HttpContext Properties

Property

Description

AllErrors

Gets an array of Exception objects, each of which represents an error that occurred while processing the request.

Application

Gets an instance of the HttpApplicationState class, which contains the global and shared states of the application.

ApplicationInstance

Gets or sets the HttpApplication object for the current request. The actual type is the global.asax code-behind class. It makes a cast to access public properties and methods you might have defined in global.asax.

Cache

Gets the ASP.NET Cache object for the current request.

Current

Gets the HttpContext object for the current request.

CurrentHandler

Gets the handler for the request that is currently being executed by the application. It is a read-only property that returns the value stored in Handler.

CurrentNotification

Indicates which event in the request pipeline is currently processing the request. It works only if the application is running in integrated pipeline mode.

Error

Gets the first exception (if any) that has been raised while processing the current request.

Handler

Gets or sets the HTTP handler for the current request.

IsCustomErrorEnabled

Indicates whether custom error handling is enabled for the current request.

IsDebuggingEnabled

Indicates whether the current request is in debug mode.

IsPostNotification

Indicates whether the current request has been processed and whether we’re in the middle of a PostXxx stage. It works only if the application is running in integrated pipeline mode.

Items

Gets a name/value collection (hash table) that can be used to share custom data and objects between HTTP modules and HTTP handlers during the request lifetime.

PreviousHandler

Gets the last handler before the current request was executed.

Profile

Gets the object that represents the profile of the current user.

Request

Gets an instance of the HttpRequest class, which represents the current HTTP request.

Response

Gets an instance of the HttpResponse class, which sends HTTP response data to the client.

Server

Gets an instance of the HttpServerUtility class, which

Return Main Page Previous Page Next Page

®Online Book Reader