HttpContext 类

定义

封装有关个别 HTTP 请求的所有 HTTP 特定的信息。

public ref class HttpContext sealed : IServiceProvider
public sealed class HttpContext : IServiceProvider
type HttpContext = class
    interface IServiceProvider
Public NotInheritable Class HttpContext
Implements IServiceProvider
继承
HttpContext
实现

示例

以下示例演示如何访问和显示对象的属性 HttpContext 。 通过使用 Context 对象的属性访问当前 HTTP 请求的 Page 上下文。

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

注解

继承和IHttpHandler接口的IHttpModule类为当前 HTTP 请求提供对HttpContext对象的引用。 该对象提供对请求的内部RequestResponse属性和Server属性的访问权限。

重要

完成时 HttpRequest ,此对象已准备好进行垃圾回收。 请求完成后的用法可能会导致未定义的行为,例如 NullReferenceException

此对象仅在由 ASP.NET 控制的线程中可用。 后台线程中的用法可能会导致未定义的行为。

构造函数

HttpContext(HttpRequest, HttpResponse)

使用指定的请求和响应对象初始化 HttpContext 类的新实例。

HttpContext(HttpWorkerRequest)

使用指定的辅助请求对象初始化 HttpContext 类的新实例。

属性

AllErrors

获取在处理 HTTP 请求的过程中累积的错误数组。

AllowAsyncDuringSyncStages

获取或设置一个值,此值指示 ASP.NET 请求处理过程中是否允许非预期的异步操作。

Application

为当前 HTTP 请求获取 HttpApplicationState 对象。

ApplicationInstance

为当前 HTTP 请求获取或设置 HttpApplication 对象。

AsyncPreloadMode

获取或设置包含适用于异步预加载模式的标志的对象。

Cache

获取当前应用程序域的 Cache 对象。

Current

为当前 HTTP 请求获取或设置 HttpContext 对象。

CurrentHandler

获取表示当前正在执行的处理程序的 IHttpHandler 对象。

CurrentNotification

获取一个 RequestNotification 值,该值指示当前正在处理的 HttpApplication 事件。

Error

获取在处理 HTTP 请求的过程中累积的第一个错误(如果有)。

Handler

获取或设置负责处理 HTTP 请求的 IHttpHandler 对象。

IsCustomErrorEnabled

获取一个值,该值指示是否已为当前 HTTP 请求启用自定义错误。

IsDebuggingEnabled

获取一个值,该值指示当前 HTTP 请求是否处于调试模式。

IsPostNotification

HttpApplication 事件完成处理之后获取 ASP.NET 管道的当前进程点的值。

IsWebSocketRequest

获取一个值,该值指示此请求是否是一个 AspNetWebSocket 请求。

IsWebSocketRequestUpgrading

获取指示连接是否从 HTTP 连接升级到 AspNetWebSocket 连接的值。

Items

获取可用于在 HTTP 请求过程中在 IHttpModule 接口和 IHttpHandler 接口之间组织和共享数据的键/值集合。

PageInstrumentation

获取此要求的页检测服务实例的引用。

PreviousHandler

获取父处理程序的 IHttpHandler 对象。

Profile

获取当前用户配置文件的 ProfileBase 对象。

Request

为当前 HTTP 请求获取 HttpRequest 对象。

Response

获取当前 HTTP 响应的 HttpResponse 对象。

Server

获取提供用于处理 Web 请求的方法的 HttpServerUtility 对象。

Session

为当前 HTTP 请求获取 HttpSessionState 对象。

SkipAuthorization

获取或设置一个值,该值指定 UrlAuthorizationModule 对象是否应跳过对当前请求的授权检查。

ThreadAbortOnTimeout

获取或设置一个值,此值指示 ASP.NET 运行时是否应在请求超时时,调用为该请求提供服务的线程上的 Abort()

Timestamp

获取当前 HTTP 请求的初始时间戳。

Trace

获取当前 HTTP 响应的 TraceContext 对象。

User

获取或设置当前 HTTP 请求的安全信息。

WebSocketNegotiatedProtocol

获取用于 AspNetWebSocket 连接而从服务器发送到客户端的协商协议。

WebSocketRequestedProtocols

获取客户要求的子协议列表的有序列表。

方法

AcceptWebSocketRequest(Func<AspNetWebSocketContext,Task>)

接受一个使用指定用户函数的 AspNetWebSocket 请求。

AcceptWebSocketRequest(Func<AspNetWebSocketContext,Task>, AspNetWebSocketOptions)

接受一个使用指定用户函数和选项对象 AspNetWebSocket 请求。

AddError(Exception)

将异常添加到当前 HTTP 请求的异常集合中。

AddOnRequestCompleted(Action<HttpContext>)

引发在请求的 HTTP 部分结束时发生的虚拟事件。

ClearError()

清除当前 HTTP 请求的所有错误。

DisposeOnPipelineCompleted(IDisposable)

使对象的 Dispose() 方法可以在此请求的 AspNetWebSocket 连接部分完成时进行调用。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetAppConfig(String)
已过时。

为当前应用程序返回请求的配置信息。

GetConfig(String)
已过时。

为当前 HTTP 请求返回所请求的配置信息。

GetGlobalResourceObject(String, String)

根据指定的 ClassKeyResourceKey 属性,获取应用程序级别资源对象。

GetGlobalResourceObject(String, String, CultureInfo)

根据指定的 ClassKeyResourceKey 属性以及 CultureInfo 对象,获取一个应用程序级别资源对象。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLocalResourceObject(String, String)

根据指定的 VirtualPathResourceKey 属性,获取页级别资源对象。

GetLocalResourceObject(String, String, CultureInfo)

根据指定的 VirtualPathResourceKey 属性以及 CultureInfo 对象,获取一个页级别资源对象。

GetSection(String)

获取当前应用程序的默认配置的指定配置节。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
RemapHandler(IHttpHandler)

用于为请求指定处理程序。

RewritePath(String)

使用给定路径重写 URL。

RewritePath(String, Boolean)

使用给定路径和一个布尔值重写 URL,该布尔值用于指定是否修改服务器资源的虚拟路径。

RewritePath(String, String, String)

使用给定的路径、路径信息和查询字符串信息重写 URL。

RewritePath(String, String, String, Boolean)

使用给定虚拟路径、路径信息、查询字符串信息和一个布尔值重写 URL,该布尔值用于指定是否将客户端文件路径设置为重写路径。

SetSessionStateBehavior(SessionStateBehavior)

设置支持 HTTP 请求所需的会话状态行为的类型。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IServiceProvider.GetService(Type)

返回当前服务类型的对象。

适用于

另请参阅