次の方法で共有


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>

注釈

IHttpModuleインターフェイスとIHttpHandler インターフェイスを継承するクラスには、現在の HTTP 要求のHttpContext オブジェクトへの参照が提供されます。 オブジェクトは、要求の組み込みの RequestResponse、および Server プロパティへのアクセスを提供します。

Important

HttpRequestが完了すると、このオブジェクトはガベージ コレクションの準備が整います。 要求の完了後に使用すると、 NullReferenceExceptionなどの未定義の動作が発生する可能性があります。

このオブジェクトは、ASP.NET によって制御されるスレッドでのみ使用できます。 バックグラウンド スレッドで使用すると、未定義の動作が発生する可能性があります。

コンストラクター

名前 説明
HttpContext(HttpRequest, HttpResponse)

指定した要求オブジェクトと応答オブジェクトを使用して、 HttpContext クラスの新しいインスタンスを初期化します。

HttpContext(HttpWorkerRequest)

指定した worker-request オブジェクトを使用する 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>, AspNetWebSocketOptions)

指定したユーザー関数と options オブジェクトを使用して、 AspNetWebSocket 要求を受け入れます。

AcceptWebSocketRequest(Func<AspNetWebSocketContext,Task>)

指定したユーザー関数を使用して AspNetWebSocket 要求を受け入れます。

AddError(Exception)

現在の HTTP 要求の例外コレクションに例外を追加します。

AddOnRequestCompleted(Action<HttpContext>)

要求の HTTP 部分が終了するときに発生する仮想イベントを発生させます。

ClearError()

現在の HTTP 要求のすべてのエラーをクリアします。

DisposeOnPipelineCompleted(IDisposable)

この要求のAspNetWebSocket接続部分が完了したときに、オブジェクトのDispose() メソッドを呼び出します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetAppConfig(String)
古い.

現在のアプリケーションの要求された構成情報を返します。

GetConfig(String)
古い.

現在の HTTP 要求に対して要求された構成情報を返します。

GetGlobalResourceObject(String, String, CultureInfo)

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

GetGlobalResourceObject(String, String)

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

GetHashCode()

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

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

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

GetLocalResourceObject(String, String)

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

GetSection(String)

現在のアプリケーションの既定の構成の指定された構成セクションを取得します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
RemapHandler(IHttpHandler)

要求のハンドラーを指定できます。

RewritePath(String, Boolean)

指定されたパスと、サーバー リソースの仮想パスが変更されるかどうかを示すブール値を使用して URL を書き換えます。

RewritePath(String, String, String, Boolean)

指定された仮想パス、パス情報、クエリ文字列情報、およびクライアント ファイル パスが書き換えパスに設定されているかどうかを指定するブール値を使用して URL を書き換えます。

RewritePath(String, String, String)

指定されたパス、パス情報、およびクエリ文字列情報を使用して URL を書き換えます。

RewritePath(String)

指定されたパスを使用して URL を書き換える。

SetSessionStateBehavior(SessionStateBehavior)

HTTP 要求をサポートするために必要なセッション状態の動作の種類を設定します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
IServiceProvider.GetService(Type)

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

適用対象

こちらもご覧ください