Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
HttpContext Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
HttpContext Class

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

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class HttpContext _
    Implements IServiceProvider
Visual Basic (Usage)
Dim instance As HttpContext
C#
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class HttpContext : IServiceProvider
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class HttpContext sealed : IServiceProvider
JScript
public final class HttpContext implements IServiceProvider

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.

TopicLocation
Developing Custom Data-Bound Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 2.0Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 2.0Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 2.0 and laterBuilding ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 2.0 and laterBuilding ASP .NET Web Applications in Visual Studio
How to: Create an Asynchronous HTTP HandlerBuilding ASP .NET Web Applications
How to: Create an Asynchronous HTTP HandlerBuilding ASP .NET Web Applications
Walkthrough: Creating a Custom Data-Bound ASP.NET Web ControlBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating an Asynchronous HTTP HandlerBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating an Asynchronous HTTP HandlerBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlBuilding Applications with Visual Web Developer
Walkthrough: Developing and Using a Custom Server ControlBuilding ASP .NET Web Applications in Visual Studio

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.

Visual Basic
<%@ 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  >
<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>

C#
<%@ 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  >
<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>

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker