Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

HttpContext Class

 

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

Namespace:   System.Web
Assembly:  System.Web (in System.Web.dll)

System.Object
  System.Web.HttpContext

public sealed class HttpContext : IServiceProvider

NameDescription
System_CAPS_pubmethodHttpContext(HttpRequest, HttpResponse)

Initializes a new instance of the HttpContext class by using the specified request and response objects.

System_CAPS_pubmethodHttpContext(HttpWorkerRequest)

Initializes a new instance of the HttpContext class that uses the specified worker-request object.

NameDescription
System_CAPS_pubpropertyAllErrors

Gets an array of errors accumulated while processing an HTTP request.

System_CAPS_pubpropertyAllowAsyncDuringSyncStages

Gets or sets a value that indicates whether asynchronous operations are allowed during parts of ASP.NET request processing when they are not expected.

System_CAPS_pubpropertyApplication

Gets the HttpApplicationState object for the current HTTP request.

System_CAPS_pubpropertyApplicationInstance

Gets or sets the HttpApplication object for the current HTTP request.

System_CAPS_pubpropertyAsyncPreloadMode

Gets or sets an object that contains flags that pertain to asynchronous preload mode.

System_CAPS_pubpropertyCache

Gets the Cache object for the current application domain.

System_CAPS_pubpropertySystem_CAPS_staticCurrent

Gets or sets the HttpContext object for the current HTTP request.

System_CAPS_pubpropertyCurrentHandler

Gets the IHttpHandler object that represents the currently executing handler.

System_CAPS_pubpropertyCurrentNotification

Gets a RequestNotification value that indicates the current HttpApplication event that is processing.

System_CAPS_pubpropertyError

Gets the first error (if any) accumulated during HTTP request processing.

System_CAPS_pubpropertyHandler

Gets or sets the IHttpHandler object responsible for processing the HTTP request.

System_CAPS_pubpropertyIsCustomErrorEnabled

Gets a value indicating whether custom errors are enabled for the current HTTP request.

System_CAPS_pubpropertyIsDebuggingEnabled

Gets a value indicating whether the current HTTP request is in debug mode.

System_CAPS_pubpropertyIsPostNotification

Gets a value that is the current processing point in the ASP.NET pipeline just after an HttpApplication event has finished processing.

System_CAPS_pubpropertyIsWebSocketRequest

Gets a value that indicates whether the request is an AspNetWebSocket request.

System_CAPS_pubpropertyIsWebSocketRequestUpgrading

Gets a value that indicates whether the connection is upgrading from an HTTP connection to an AspNetWebSocket connection.

System_CAPS_pubpropertyItems

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.

System_CAPS_pubpropertyPageInstrumentation

Gets a reference to the page-instrumentation service instance for this request.

System_CAPS_pubpropertyPreviousHandler

Gets the IHttpHandler object for the parent handler.

System_CAPS_pubpropertyProfile

Gets the ProfileBase object for the current user profile.

System_CAPS_pubpropertyRequest

Gets the HttpRequest object for the current HTTP request.

System_CAPS_pubpropertyResponse

Gets the HttpResponse object for the current HTTP response.

System_CAPS_pubpropertyServer

Gets the HttpServerUtility object that provides methods used in processing Web requests.

System_CAPS_pubpropertySession

Gets the HttpSessionState object for the current HTTP request.

System_CAPS_pubpropertySkipAuthorization

Gets or sets a value that specifies whether the UrlAuthorizationModule object should skip the authorization check for the current request.

System_CAPS_pubpropertyThreadAbortOnTimeout

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.

System_CAPS_pubpropertyTimestamp

Gets the initial timestamp of the current HTTP request.

System_CAPS_pubpropertyTrace

Gets the TraceContext object for the current HTTP response.

System_CAPS_pubpropertyUser

Gets or sets security information for the current HTTP request.

System_CAPS_pubpropertyWebSocketNegotiatedProtocol

Gets the negotiated protocol that was sent from the server to the client for an AspNetWebSocket connection.

System_CAPS_pubpropertyWebSocketRequestedProtocols

Gets the ordered list of protocols requested by the client.

NameDescription
System_CAPS_pubmethodAcceptWebSocketRequest(Func<AspNetWebSocketContext, Task>)

Accepts an AspNetWebSocket request using the specified user function.

System_CAPS_pubmethodAcceptWebSocketRequest(Func<AspNetWebSocketContext, Task>, AspNetWebSocketOptions)

Accepts an AspNetWebSocket request using the specified user function and options object.

System_CAPS_pubmethodAddError(Exception)

Adds an exception to the exception collection for the current HTTP request.

System_CAPS_pubmethodAddOnRequestCompleted(Action<HttpContext>)

Raises a virtual event that occurs when the HTTP part of the request is ending.

System_CAPS_pubmethodClearError()

Clears all errors for the current HTTP request.

System_CAPS_pubmethodDisposeOnPipelineCompleted(IDisposable)

Enables an object's Dispose method to be called when the AspNetWebSocket connection part of this request is completed.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticGetAppConfig(String)

Obsolete.Returns requested configuration information for the current application.

System_CAPS_pubmethodGetConfig(String)

Obsolete.Returns requested configuration information for the current HTTP request.

System_CAPS_pubmethodSystem_CAPS_staticGetGlobalResourceObject(String, String)

Gets an application-level resource object based on the specified ClassKey and ResourceKey properties.

System_CAPS_pubmethodSystem_CAPS_staticGetGlobalResourceObject(String, String, CultureInfo)

Gets an application-level resource object based on the specified ClassKey and ResourceKey properties, and on the CultureInfo object.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticGetLocalResourceObject(String, String)

Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties.

System_CAPS_pubmethodSystem_CAPS_staticGetLocalResourceObject(String, String, CultureInfo)

Gets a page-level resource object based on the specified VirtualPath and ResourceKey properties, and on the CultureInfo object.

System_CAPS_pubmethodGetSection(String)

Gets a specified configuration section for the current application's default configuration.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodRemapHandler(IHttpHandler)

Enables you to specify a handler for the request.

System_CAPS_pubmethodRewritePath(String)

Rewrites the URL using the given path.

System_CAPS_pubmethodRewritePath(String, Boolean)

Rewrites the URL using the given path and a Boolean value that specifies whether the virtual path for server resources is modified.

System_CAPS_pubmethodRewritePath(String, String, String)

Rewrites the URL by using the given path, path information, and query string information.

System_CAPS_pubmethodRewritePath(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.

System_CAPS_pubmethodSetSessionStateBehavior(SessionStateBehavior)

Sets the type of session state behavior that is required in order to support an HTTP request.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIServiceProvider.GetService(Type)

This API supports the product infrastructure and is not intended to be used directly from your code. 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>

.NET Framework
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft