Export (0) Print
Expand All

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 sealed class HttpContext : IServiceProvider

The HttpContext type exposes the following members.

  NameDescription
Public methodHttpContext(HttpWorkerRequest)Initializes a new instance of the HttpContext class that uses the specified worker-request object.
Public methodHttpContext(HttpRequest, HttpResponse)Initializes a new instance of the HttpContext class by using the specified request and response objects.
Top

  NameDescription
Public propertyAllErrorsGets an array of errors accumulated while processing an HTTP request.
Public propertyAllowAsyncDuringSyncStagesGets or sets a value that indicates whether asynchronous operations are allowed during parts of ASP.NET request processing when they are not expected.
Public propertyApplicationGets the HttpApplicationState object for the current HTTP request.
Public propertyApplicationInstanceGets or sets the HttpApplication object for the current HTTP request.
Public propertyAsyncPreloadModeGets or sets an object that contains flags that pertain to asynchronous preload mode.
Public propertyCacheGets the Cache object for the current application domain.
Public propertyStatic memberCurrentGets or sets the HttpContext object for the current HTTP request.
Public propertyCurrentHandlerGets the IHttpHandler object that represents the currently executing handler.
Public propertyCurrentNotificationGets a RequestNotification value that indicates the current HttpApplication event that is processing.
Public propertyErrorGets the first error (if any) accumulated during HTTP request processing.
Public propertyHandlerGets or sets the IHttpHandler object responsible for processing the HTTP request.
Public propertyIsCustomErrorEnabledGets a value indicating whether custom errors are enabled for the current HTTP request.
Public propertyIsDebuggingEnabledGets a value indicating whether the current HTTP request is in debug mode.
Public propertyIsPostNotificationGets a value that is the current processing point in the ASP.NET pipeline just after an HttpApplication event has finished processing.
Public propertyIsWebSocketRequestGets a value that indicates whether the request is an AspNetWebSocket request.
Public propertyIsWebSocketRequestUpgradingGets a value that indicates whether the connection is upgrading from an HTTP connection to an AspNetWebSocket connection.
Public propertyItemsGets 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 propertyPageInstrumentationGets a reference to the page-instrumentation service instance for this request.
Public propertyPreviousHandlerGets the IHttpHandler object for the parent handler.
Public propertyProfileGets the ProfileBase object for the current user profile.
Public propertyRequestGets the HttpRequest object for the current HTTP request.
Public propertyResponseGets the HttpResponse object for the current HTTP response.
Public propertyServerGets the HttpServerUtility object that provides methods used in processing Web requests.
Public propertySessionGets the HttpSessionState object for the current HTTP request.
Public propertySkipAuthorizationGets or sets a value that specifies whether the UrlAuthorizationModule object should skip the authorization check for the current request.
Public propertyThreadAbortOnTimeoutGets 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 propertyTimestampGets the initial timestamp of the current HTTP request.
Public propertyTraceGets the TraceContext object for the current HTTP response.
Public propertyUserGets or sets security information for the current HTTP request.
Public propertyWebSocketNegotiatedProtocolGets the negotiated protocol that was sent from the server to the client for an AspNetWebSocket connection.
Public propertyWebSocketRequestedProtocolsGets the ordered list of protocols requested by the client.
Top

  NameDescription
Public methodAcceptWebSocketRequest(Func<AspNetWebSocketContext, Task>)Accepts an AspNetWebSocket request using the specified user function.
Public methodAcceptWebSocketRequest(Func<AspNetWebSocketContext, Task>, AspNetWebSocketOptions)Accepts an AspNetWebSocket request using the specified user function and options object.
Public methodAddErrorAdds an exception to the exception collection for the current HTTP request.
Public methodAddOnRequestCompletedRaises a virtual event that occurs when the HTTP part of the request is ending.
Public methodClearErrorClears all errors for the current HTTP request.
Public methodDisposeOnPipelineCompletedEnables an object's Dispose method to be called when the AspNetWebSocket connection part of this request is completed.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodStatic memberGetAppConfig Obsolete. Returns requested configuration information for the current application.
Public methodGetConfig Obsolete. Returns requested configuration information for the current HTTP request.
Public methodStatic memberGetGlobalResourceObject(String, String)Gets an application-level resource object based on the specified ClassKey and ResourceKey properties.
Public methodStatic memberGetGlobalResourceObject(String, String, CultureInfo)Gets an application-level resource object based on the specified ClassKey and ResourceKey properties, and on the CultureInfo object.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodStatic memberGetLocalResourceObject(String, String)Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties.
Public methodStatic memberGetLocalResourceObject(String, String, CultureInfo)Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties, and on the CultureInfo object.
Public methodGetSectionGets a specified configuration section for the current application's default configuration.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodRemapHandlerEnables you to specify a handler for the request.
Public methodRewritePath(String)Rewrites the URL using the given path.
Public methodRewritePath(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 methodRewritePath(String, String, String)Rewrites the URL by using the given path, path information, and query string information.
Public methodRewritePath(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 methodSetSessionStateBehaviorSets the type of session state behavior that is required in order to support an HTTP request.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIServiceProvider.GetServiceInfrastructure. 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="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>

.NET Framework

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

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.
Show:
© 2014 Microsoft