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 。 使用 物件的 屬性 Page 來存取 Context 目前 HTTP 要求的內容。

<%@ 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 類別會提供目前 HTTP 要求的 物件參考 HttpContextIHttpHandler 物件可讓您存取要求的內建 RequestResponseServer 屬性。

重要

此物件已準備好在 完成時 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

取得 RequestNotification 值,表示目前正在處理的 HttpApplication 事件。

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

取得 HttpServerUtility 物件,這個物件提供用於處理 Web 要求的方法。

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)

允許在這個要求的 AspNetWebSocket 連接部分完成時,呼叫物件的 Dispose() 方法。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetAppConfig(String)
已過時。

傳回目前應用程式的要求組態資訊。

GetConfig(String)
已過時。

傳回目前 HTTP 要求的要求組態資訊。

GetGlobalResourceObject(String, String)

根據指定的 ClassKeyResourceKey 屬性,取得應用程式層級資源物件。

GetGlobalResourceObject(String, String, CultureInfo)

根據指定的 ClassKeyResourceKey 屬性以及 CultureInfo 物件,取得應用程式層級資源物件。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLocalResourceObject(String, String)

根據指定的 VirtualPathResourceKey 屬性,取得頁面層級資源物件。

GetLocalResourceObject(String, String, CultureInfo)

根據指定的 VirtualPathResourceKey 屬性以及 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)

傳回目前服務類型的物件。

適用於

另請參閱