HttpContext クラス
それぞれの HTTP 要求に関する HTTP 固有のすべての情報をカプセル化します。
名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)
構文
'宣言
Public NotInheritable Class HttpContext
Implements IServiceProvider
'使用
Dim instance As HttpContext
public sealed class HttpContext : IServiceProvider
public ref class HttpContext sealed : IServiceProvider
public final class HttpContext implements IServiceProvider
public final class HttpContext implements IServiceProvider
適用できません。
解説
IHttpModule インターフェイスおよび IHttpHandler インターフェイスを継承するクラスには、現在の HTTP 要求の HttpContext オブジェクトへの参照が用意されています。このオブジェクトは、要求に組み込まれる Request、Response、および Server 各プロパティへのアクセスを実現します。
使用例
HttpContext オブジェクトのプロパティにアクセスして表示する方法を次のコード例に示します。現在の HTTP 要求のコンテキストにアクセスするには、Page オブジェクトの Context プロパティを使用します。
<%@ 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>
<%@ 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 のセキュリティ
- AspNetHostingPermission (ホスト環境での動作に必要なアクセス許可)。要求値 : LinkDemand。アクセス許可値 : Minimal。
継承階層
System.Object
System.Web.HttpContext
スレッド セーフ
この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。
プラットフォーム
Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0