4 out of 12 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.

  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 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 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 indicates whether the current ASP.NET event is considered a post event.
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 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 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.
Top
  Name Description
Public method AddError Adds an exception to the exception collection for the current HTTP request.
Public method ClearError Clears all errors for the current HTTP request.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (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 a hash function for a particular type. (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.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (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="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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Useless example - illegal syntax
I've no idea what the syntax for this should be, but this isn't it! Perhaps the page should have had an import which is missing.