Export (0) Print
Expand All
5 out of 11 rated this helpful - Rate this topic

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 propertyApplicationGets the HttpApplicationState object for the current HTTP request.
Public propertyApplicationInstanceGets or sets the HttpApplication object for the current HTTP request.
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 indicates whether the current ASP.NET event is considered a post event.
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 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 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.
Top
  NameDescription
Public methodAddErrorAdds an exception to the exception collection for the current HTTP request.
Public methodClearErrorClears all errors for the current HTTP request.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (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 a hash function for a particular type. (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.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (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, 3.5, 3.0, 2.0, 1.1, 1.0

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.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.