HttpContext Class
Encapsulates all HTTP-specific information about an individual HTTP request.
Assembly: System.Web (in System.Web.dll)
The HttpContext type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | HttpContext(HttpWorkerRequest) | Initializes a new instance of the HttpContext class that uses the specified worker-request object. |
![]() | HttpContext(HttpRequest, HttpResponse) | Initializes a new instance of the HttpContext class by using the specified request and response objects. |
| Name | Description | |
|---|---|---|
![]() | AllErrors | Gets an array of errors accumulated while processing an HTTP request. |
![]() | Application | Gets the HttpApplicationState object for the current HTTP request. |
![]() | ApplicationInstance | Gets or sets the HttpApplication object for the current HTTP request. |
![]() | Cache | Gets the Cache object for the current application domain. |
![]() ![]() | Current | Gets or sets the HttpContext object for the current HTTP request. |
![]() | CurrentHandler | Gets the IHttpHandler object that represents the currently executing handler. |
![]() | CurrentNotification | Gets a RequestNotification value that indicates the current HttpApplication event that is processing. |
![]() | Error | Gets the first error (if any) accumulated during HTTP request processing. |
![]() | Handler | Gets or sets the IHttpHandler object responsible for processing the HTTP request. |
![]() | IsCustomErrorEnabled | Gets a value indicating whether custom errors are enabled for the current HTTP request. |
![]() | IsDebuggingEnabled | Gets a value indicating whether the current HTTP request is in debug mode. |
![]() | IsPostNotification | Gets a value that indicates whether the current ASP.NET event is considered a post event. |
![]() | Items | Gets a key/value collection that can be used to organize and share data between an IHttpModule interface and an IHttpHandler interface during an HTTP request. |
![]() | PreviousHandler | Gets the IHttpHandler object for the parent handler. |
![]() | Profile | Gets the ProfileBase object for the current user profile. |
![]() | Request | Gets the HttpRequest object for the current HTTP request. |
![]() | Response | Gets the HttpResponse object for the current HTTP response. |
![]() | Server | Gets the HttpServerUtility object that provides methods used in processing Web requests. |
![]() | Session | Gets the HttpSessionState object for the current HTTP request. |
![]() | SkipAuthorization | Gets or sets a value that specifies whether the UrlAuthorizationModule object should skip the authorization check for the current request. |
![]() | Timestamp | Gets the initial timestamp of the current HTTP request. |
![]() | Trace | Gets the TraceContext object for the current HTTP response. |
![]() | User | Gets or sets security information for the current HTTP request. |
| Name | Description | |
|---|---|---|
![]() | AddError | Adds an exception to the exception collection for the current HTTP request. |
![]() | ClearError | Clears all errors for the current HTTP request. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | GetAppConfig | Obsolete. Returns requested configuration information for the current application. |
![]() | GetConfig | Obsolete. Returns requested configuration information for the current HTTP request. |
![]() ![]() | GetGlobalResourceObject(String, String) | Gets an application-level resource object based on the specified ClassKey and ResourceKey properties. |
![]() ![]() | GetGlobalResourceObject(String, String, CultureInfo) | Gets an application-level resource object based on the specified ClassKey and ResourceKey properties, and on the CultureInfo object. |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() | GetLocalResourceObject(String, String) | Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties. |
![]() ![]() | GetLocalResourceObject(String, String, CultureInfo) | Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties, and on the CultureInfo object. |
![]() | GetSection | Gets a specified configuration section for the current application's default configuration. |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | RemapHandler | Enables you to specify a handler for the request. |
![]() | RewritePath(String) | Rewrites the URL using the given path. |
![]() | RewritePath(String, Boolean) | Rewrites the URL using the given path and a Boolean value that specifies whether the virtual path for server resources is modified. |
![]() | RewritePath(String, String, String) | Rewrites the URL by using the given path, path information, and query string information. |
![]() | RewritePath(String, String, String, Boolean) | Rewrites the URL using the given virtual path, path information, query string information, and a Boolean value that specifies whether the client file path is set to the rewrite path. |
![]() | SetSessionStateBehavior | Sets the type of session state behavior that is required in order to support an HTTP request. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | IServiceProvider.GetService | Infrastructure. Returns an object for the current service type. |
Classes that inherit the IHttpModule and IHttpHandler interfaces are provided a reference to an HttpContext object for the current HTTP request. The object provides access to the intrinsic Request, Response, and Server properties for the request.
A Visual Studio Web site project with source code is available to accompany this topic: Download.
The following example demonstrates how to access and display properties of the HttpContext object. The context of the current HTTP request is accessed by using the Context property of the Page object.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { // The HttpContext associated with the page can be accessed by the Context property. System.Text.StringBuilder sb = new System.Text.StringBuilder(); // Use the current HttpContext object to determine if custom errors are enabled. sb.Append("Is custom errors enabled: " + Context.IsCustomErrorEnabled.ToString() + "<br/>"); // Use the current HttpContext object to determine if debugging is enabled. sb.Append("Is debugging enabled: " + Context.IsDebuggingEnabled.ToString() + "<br/>"); // Use the current HttpContext object to access the current TraceContext object. sb.Append("Trace Enabled: " + Context.Trace.IsEnabled.ToString() + "<br/>"); // Use the current HttpContext object to access the current HttpApplicationState object. sb.Append("Number of items in Application state: " + Context.Application.Count.ToString() + "<br/>"); // Use the current HttpContext object to access the current HttpSessionState object. // Session state may not be configured. try { sb.Append("Number of items in Session state: " + Context.Session.Count.ToString() + "<br/>"); } catch { sb.Append("Session state not enabled. <br/>"); } // Use the current HttpContext object to access the current Cache object. sb.Append("Number of items in the cache: " + Context.Cache.Count.ToString() + "<br/>"); // Use the current HttpContext object to determine the timestamp for the current HTTP Request. sb.Append("Timestamp for the HTTP request: " + Context.Timestamp.ToString() + "<br/>"); // Assign StringBuilder object to output label. OutputLabel.Text = sb.ToString(); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>HttpContext Example</title> </head> <body> <form id="form1" runat="server"> <div> Using the current HttpContext to get information about the current page. <br /> <asp:Label id="OutputLabel" runat="server"></asp:Label> </div> </form> </body> </html>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
