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 示します。 現在の HTTP 要求のコンテキストには、オブジェクトのPageプロパティをContext使用してアクセスします。

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

処理中の現在の HttpApplication イベントを示す RequestNotification 値を取得します。

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)

アプリケーション レベル リソース オブジェクトを、指定した ClassKey プロパティおよび ResourceKey プロパティに基づいて取得します。

GetGlobalResourceObject(String, String, CultureInfo)

アプリケーション レベル リソース オブジェクトを、指定した ClassKey プロパティおよび ResourceKey プロパティ、および CultureInfo オブジェクトに基づいて取得します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLocalResourceObject(String, String)

ページ レベル リソース オブジェクトを、指定した VirtualPath プロパティおよび ResourceKey プロパティに基づいて取得します。

GetLocalResourceObject(String, String, CultureInfo)

ページ レベル リソース オブジェクトを、指定した VirtualPath プロパティおよび ResourceKey プロパティ、および 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)

現在のサービス型のオブジェクトを返します。

適用対象

こちらもご覧ください