Programming Microsoft ASP.NET 4 - Dino Esposito [336]
Session
Gets an instance of the HttpSessionState class, which manages session-specific data.
SkipAuthorization
Gets or sets a Boolean value that specifies whether the URL-based authorization module will skip the authorization check for the current request. This is false by default. It is mostly used by authentication modules that need to redirect to a page that allows anonymous access.
Timestamp
Gets a DateTime object that represents the initial timestamp of the current request.
Trace
Gets the TraceContext object for the current response.
User
Gets or sets the IPrincipal object that represents the identity of the user making the request.
The Current property is a frequently used static member that returns the HttpContext object for the request being processed.
The Items property is a dictionary object—a hash table, to be exact—that can be used to share information between the modules and handlers involved with the particular request. By using this property, each custom HTTP module or handler can add its own information to the HttpContext object serving the request. The information stored in Items is ultimately made available to the page. The lifetime of this information is limited to the request.
Methods of the HttpContext Class
Table 16-7 lists the methods specific to the HttpContext class.
Table 16-7. HttpContext Methods
Method
Description
AddError
Adds an exception object to the AllErrors collection.
ClearError
Clears all errors for the current request.
GetAppConfig
Returns requested configuration information for the current application. The information is collected from machine.config and the application’s main web.config files. It is marked as obsolete in ASP.NET 4.0.
GetConfig
Returns requested configuration information for the current request. The information is collected at the level of the requested URL, taking into account any child web.config files defined in subdirectories. It is marked as obsolete in ASP.NET 4.0.
GetGlobalResourceObject
Loads a global resource.
GetLocalResourceObject
Loads a local, page-specific resource.
GetSection
Returns requested configuration information for the current request.
RemapHandler
Allows you to programmatically set the handler to serve the request. It must be invoked before the runtime reaches the MapRequestHandler stage. If the Handler property of HttpContext is not null at that stage, the runtime defaults to it.
RewritePath
Mostly for internal use; overwrites URL and the query string of the current Request object.
SetSessionStateBehavior
Allows you to programmatically set the expected behavior for the session state—either read-only, read-write, or no session. It must be called before the AcquireRequestState event fires.
Over time, the GetSection method has replaced GetConfig, which has been marked as obsolete and should not be used. If you have old code using GetConfig, just change the name of the method. The prototype is the same. Also, GetAppConfig is marked as obsolete in ASP.NET 4. It has been replaced by GetWebApplicationSection, a static member of the new WebConfigurationManager class. Also, in this case, no changes are required to be made to the prototype. Let’s spend a few more words to dig out some interesting characteristics of other methods of the HttpContext class.
URL Rewriting
The RewritePath method lets you change the URL of the current request on the fly, thus performing a sort of internal redirect. As a result, the displayed page is the one you set through RewritePath; the page shown in the address bar remains the originally requested one. The change of the final URL takes place on the server and, more importantly, within the context of the same call. RewritePath should be used carefully and mainly from within the global.asax file. If you use RewritePath in the context of a postback event, you can experience some view-state problems.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var context