Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
User Action:
.NET Framework Class Library

HttpContext Class

Encapsulates all HTTP-specific information about an individual HTTP request.

System.Object
  System.Web.HttpContext

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
Public NotInheritable Class HttpContext _
	Implements IServiceProvider

The HttpContext type exposes the following members.

  Name Description
Public method HttpContext(HttpWorkerRequest) Initializes a new instance of the HttpContext class that uses the specified worker-request object.
Public method HttpContext(HttpRequest, HttpResponse) Initializes a new instance of the HttpContext class by using the specified request and response objects.
Top
  Name Description
Public property AllErrors Gets an array of errors accumulated while processing an HTTP request.
Public property AllowAsyncDuringSyncStages Gets or sets a value that indicates whether asynchronous operations are allowed during parts of ASP.NET request processing when they are not expected.
Public property Application Gets the HttpApplicationState object for the current HTTP request.
Public property ApplicationInstance Gets or sets the HttpApplication object for the current HTTP request.
Public property AsyncPreloadMode Gets or sets an object that contains flags that pertain to asynchronous preload mode.
Public property Cache Gets the Cache object for the current application domain.
Public property Static member Current Gets or sets the HttpContext object for the current HTTP request.
Public property CurrentHandler Gets the IHttpHandler object that represents the currently executing handler.
Public property CurrentNotification Gets a RequestNotification value that indicates the current HttpApplication event that is processing.
Public property Error Gets the first error (if any) accumulated during HTTP request processing.
Public property Handler Gets or sets the IHttpHandler object responsible for processing the HTTP request.
Public property IsCustomErrorEnabled Gets a value indicating whether custom errors are enabled for the current HTTP request.
Public property IsDebuggingEnabled Gets a value indicating whether the current HTTP request is in debug mode.
Public property IsPostNotification Gets a value that is the current processing point in the ASP.NET pipeline just after an HttpApplication event has finished processing.
Public property IsWebSocketRequest Gets a value that indicates whether the request is an AspNetWebSocket request.
Public property IsWebSocketRequestUpgrading Gets a value that indicates whether the connection is upgrading from an HTTP connection to an AspNetWebSocket connection.
Public property 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.
Public property PageInstrumentation Gets a reference to the page-instrumentation service instance for this request.
Public property PreviousHandler Gets the IHttpHandler object for the parent handler.
Public property Profile Gets the ProfileBase object for the current user profile.
Public property Request Gets the HttpRequest object for the current HTTP request.
Public property Response Gets the HttpResponse object for the current HTTP response.
Public property Server Gets the HttpServerUtility object that provides methods used in processing Web requests.
Public property Session Gets the HttpSessionState object for the current HTTP request.
Public property SkipAuthorization Gets or sets a value that specifies whether the UrlAuthorizationModule object should skip the authorization check for the current request.
Public property ThreadAbortOnTimeout Gets or sets a value that specifies whether the ASP.NET runtime should call Thread.Abort on the thread that is servicing this request when the request times out.
Public property Timestamp Gets the initial timestamp of the current HTTP request.
Public property Trace Gets the TraceContext object for the current HTTP response.
Public property User Gets or sets security information for the current HTTP request.
Public property WebSocketNegotiatedProtocol Gets the negotiated protocol that was sent from the server to the client for an AspNetWebSocket connection.
Public property WebSocketRequestedProtocols Gets the ordered list of protocols requested by the client.
Top
  Name Description
Public method AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task>) Accepts an AspNetWebSocket request using the specified user function.
Public method AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task>, AspNetWebSocketOptions) Accepts an AspNetWebSocket request using the specified user function and options object.
Public method AddError Adds an exception to the exception collection for the current HTTP request.
Public method AddOnRequestCompleted Raises a virtual event that occurs when the HTTP part of the request is ending.
Public method ClearError Clears all errors for the current HTTP request.
Public method DisposeOnPipelineCompleted Enables an object's Dispose method to be called when the AspNetWebSocket connection part of this request is completed.
Public method Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method Static member GetAppConfig Obsolete. Returns requested configuration information for the current application.
Public method GetConfig Obsolete. Returns requested configuration information for the current HTTP request.
Public method Static member GetGlobalResourceObject(String, String) Gets an application-level resource object based on the specified ClassKey and ResourceKey properties.
Public method Static member GetGlobalResourceObject(String, String, CultureInfo) Gets an application-level resource object based on the specified ClassKey and ResourceKey properties, and on the CultureInfo object.
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method Static member GetLocalResourceObject(String, String) Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties.
Public method Static member GetLocalResourceObject(String, String, CultureInfo) Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties, and on the CultureInfo object.
Public method GetSection Gets a specified configuration section for the current application's default configuration.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method RemapHandler Enables you to specify a handler for the request.
Public method RewritePath(String) Rewrites the URL using the given path.
Public method 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.
Public method RewritePath(String, String, String) Rewrites the URL by using the given path, path information, and query string information.
Public method 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.
Public method SetSessionStateBehavior Sets the type of session state behavior that is required in order to support an HTTP request.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Explicit interface implemetation Private method IServiceProvider.GetService Infrastructure. Returns an object for the current service type.
Top

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="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' The HttpContext associated with the page can be accessed by the Context property.
        Dim sb As 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 ex As Exception
            sb.Append("Session state not enabled. <br/>")
        End Try

        ' 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()
    End Sub
</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>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.