更新:2007 年 11 月
封裝所有關於個別 HTTP 要求的 HTTP 特定資訊。
命名空間:
System.Web 組件:
System.Web (在 System.Web.dll 中)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class HttpContext _
Implements IServiceProvider
Dim instance As HttpContext
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class HttpContext : IServiceProvider
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class HttpContext sealed : IServiceProvider
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public final class HttpContext implements IServiceProvider
public final class HttpContext implements IServiceProvider
繼承 IHttpModule 和 IHttpHandler 介面的類別具備對目前 HTTP 要求的 HttpContext 物件的參考。物件針對要求提供內建的 Request、Response 和 Server 屬性的存取。
HttpContext 是在 .NET Framework 3.5 版中引入。如需詳細資訊,請參閱 .NET Framework 3.5 架構。
下列範例示範如何存取和顯示 HttpContext 物件的屬性。使用 Page 物件的 Context 屬性,可以存取目前 HTTP 要求的內容。
<%@ 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>
<%@ 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
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5、3.0、2.0、1.1、1.0
參考