HttpContext 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
封装有关个别 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>
	注解
为继承 IHttpModule 和 IHttpHandler 接口的类提供对 HttpContext 当前 HTTP 请求的 对象的引用。 对象提供对请求的内部 Request、 Response和 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 | 
		 获取客户要求的子协议列表的有序列表。  | 
        	
方法
显式接口实现
| IServiceProvider.GetService(Type) | 
		 返回当前服务类型的对象。  |