HttpContextとは? わかりやすく解説

HttpContext クラス

それぞれの HTTP 要求に関する HTTP 固有のすべての情報をカプセル化します。

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public NotInheritable Class
 HttpContext
    Implements IServiceProvider
public sealed class HttpContext : IServiceProvider
public ref class HttpContext sealed : IServiceProvider
public final class HttpContext implements IServiceProvider
public final class HttpContext implements IServiceProvider
解説解説
使用例使用例

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  >
<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) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
HttpContext メンバ
System.Web 名前空間
IHttpModule
IHttpHandler

HttpContext コンストラクタ (HttpWorkerRequest)


HttpContext コンストラクタ (HttpRequest, HttpResponse)

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

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)
構文構文

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

HttpContext コンストラクタ


HttpContext プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ CurrentHandler 現在実行中のハンドラを表す IHttpHandler オブジェクトを取得します。
パブリック プロパティ Error HTTP 要求の処理中にエラーが蓄積された場合は、その最初のエラーを取得します。
パブリック プロパティ Handler HTTP 要求の処理を実行する IHttpHandler オブジェクトを取得または設定します。
パブリック プロパティ IsCustomErrorEnabled 現在の HTTP 要求に対してカスタム エラーが有効かどうかを示す値を取得します。
パブリック プロパティ IsDebuggingEnabled 現在の HTTP 要求がデバッグ モードかどうかを示す値を取得します。
パブリック プロパティ Items HTTP 要求時に IHttpModule インターフェイスと IHttpHandler インターフェイスとの間でデータを編成および共有するために使用できるキー/値のコレクションを取得します。
パブリック プロパティ PreviousHandler 親ハンドラの IHttpHandler オブジェクトを取得します。
パブリック プロパティ Profile 現在のユーザー プロファイルの ProfileBase オブジェクトを取得します。
パブリック プロパティ Request 現在の HTTP 要求に対する HttpRequest オブジェクトを取得します。
パブリック プロパティ Response 現在の HTTP 応答に対する HttpResponse オブジェクトを取得します。
パブリック プロパティ Server Web 要求の処理で使用されるメソッドを提供する HttpServerUtility オブジェクトを取得します。
パブリック プロパティ Session 現在の HTTP 要求に対する HttpSessionState オブジェクトを取得します。
パブリック プロパティ SkipAuthorization UrlAuthorizationModule オブジェクトが現在の要求に対する承認チェックをスキップするかどうかを指定する値を取得または設定します。
パブリック プロパティ Timestamp 現在の HTTP 要求の初期タイムスタンプを取得します。
パブリック プロパティ Trace 現在の HTTP 応答に対する TraceContext オブジェクトを取得します。
パブリック プロパティ User 現在の HTTP 要求のセキュリティ情報を取得または設定します。
参照参照

関連項目

HttpContext クラス
System.Web 名前空間
IHttpModule
IHttpHandler

HttpContext メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド AddError 現在の HTTP 要求の例外コレクションに例外を追加します。
パブリック メソッド ClearError 現在の HTTP 要求に対するすべてのエラーをクリアします。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetAppConfig 現在のアプリケーションに対して要求された構成情報を返します。
パブリック メソッド GetConfig 現在の HTTP 要求に対して要求された構成情報を返します。
パブリック メソッド GetGlobalResourceObject オーバーロードされます。 アプリケーション レベル リソースを取得します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetLocalResourceObject オーバーロードされます。 ページ レベル リソースを取得します。
パブリック メソッド GetSection 現在のアプリケーションの既定構成の、指定した構成セクションを取得します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド RewritePath オーバーロードされます。 内部リライト パスを割り当てます。これにより、要求された URL とは異なる内部パスのリソースを示すことができます。RewritePath は Cookie なしのセッション状態で使用されます。
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.IServiceProvider.GetService 現在のサービス型のオブジェクトを返します。
参照参照

関連項目

HttpContext クラス
System.Web 名前空間
IHttpModule
IHttpHandler

HttpContext メンバ

それぞれの HTTP 要求に関する HTTP 固有のすべての情報をカプセル化します。

HttpContext データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ CurrentHandler 現在実行中のハンドラを表す IHttpHandler オブジェクトを取得します。
パブリック プロパティ Error HTTP 要求の処理中にエラーが蓄積された場合は、その最初のエラーを取得します。
パブリック プロパティ Handler HTTP 要求の処理を実行する IHttpHandler オブジェクトを取得または設定します。
パブリック プロパティ IsCustomErrorEnabled 現在の HTTP 要求に対してカスタム エラーが有効かどうかを示す値を取得します。
パブリック プロパティ IsDebuggingEnabled 現在の HTTP 要求がデバッグ モードかどうかを示す値を取得します。
パブリック プロパティ Items HTTP 要求時に IHttpModule インターフェイスと IHttpHandler インターフェイスとの間でデータを編成および共有するために使用できるキー/値のコレクションを取得します。
パブリック プロパティ PreviousHandler 親ハンドラの IHttpHandler オブジェクトを取得します。
パブリック プロパティ Profile 現在のユーザー プロファイルの ProfileBase オブジェクトを取得します。
パブリック プロパティ Request 現在の HTTP 要求に対する HttpRequest オブジェクトを取得します。
パブリック プロパティ Response 現在の HTTP 応答に対する HttpResponse オブジェクトを取得します。
パブリック プロパティ Server Web 要求の処理で使用されるメソッドを提供する HttpServerUtility オブジェクトを取得します。
パブリック プロパティ Session 現在の HTTP 要求に対する HttpSessionState オブジェクトを取得します。
パブリック プロパティ SkipAuthorization UrlAuthorizationModule オブジェクトが現在の要求に対する承認チェックをスキップするかどうかを指定する値を取得または設定します。
パブリック プロパティ Timestamp 現在の HTTP 要求の初期タイムスタンプを取得します。
パブリック プロパティ Trace 現在の HTTP 応答に対する TraceContext オブジェクトを取得します。
パブリック プロパティ User 現在の HTTP 要求のセキュリティ情報を取得または設定します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド AddError 現在の HTTP 要求の例外コレクションに例外を追加します。
パブリック メソッド ClearError 現在の HTTP 要求に対するすべてのエラーをクリアします。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetAppConfig 現在のアプリケーションに対して要求された構成情報を返します。
パブリック メソッド GetConfig 現在の HTTP 要求に対して要求された構成情報を返します。
パブリック メソッド GetGlobalResourceObject オーバーロードされます。 アプリケーション レベル リソースを取得します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetLocalResourceObject オーバーロードされます。 ページ レベル リソースを取得します。
パブリック メソッド GetSection 現在のアプリケーションの既定構成の、指定した構成セクションを取得します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド RewritePath オーバーロードされます。 内部リライト パスを割り当てます。これにより、要求された URL とは異なる内部パスのリソースを示すことができます。RewritePath は Cookie なしのセッション状態で使用されます。
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.IServiceProvider.GetService 現在のサービス型のオブジェクトを返します。
参照参照

関連項目

HttpContext クラス
System.Web 名前空間
IHttpModule
IHttpHandler



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「HttpContext」の関連用語

HttpContextのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HttpContextのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS