共用方式為


HttpContext.Current 屬性

定義

取得或設定目前 HTTP 要求的 HttpContext 物件。

public:
 static property System::Web::HttpContext ^ Current { System::Web::HttpContext ^ get(); void set(System::Web::HttpContext ^ value); };
public static System.Web.HttpContext Current { get; set; }
member this.Current : System.Web.HttpContext with get, set
Public Shared Property Current As HttpContext

屬性值

HttpContext

目前 HTTP 要求的 HttpContext 執行個體。

範例

下列程式碼範例會 Current 使用 屬性來存取 HttpContext.AddErrorHttpContext.ClearError 方法和 HttpContext.AllErrors 屬性。 此範例會使用 AddError 方法建立三個自訂例外狀況,並使用 AllErrors 屬性將這些例外狀況載入陣列。 然後,它會將陣列寫入包含的頁面,並使用 ClearError 方法來清除 屬性中的所有錯誤 Context

protected void Page_Load(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;
    Response.Write("<p>HttpContext.Current Example:</p>");

    // Add three custom exceptions.
    context.AddError(new Exception("New Exception #1"));
    context.AddError(new Exception("New Exception #2"));
    context.AddError(new Exception("New Exception #3"));

    // Capture all the new Exceptions in an array.
    Exception[] errs = context.AllErrors;

    foreach (Exception ex in errs)
    {
        Response.Write("<p>" + Server.HtmlEncode(ex.ToString()) + "</p>");
    }

    // Clear the exceptions so ASP.NET won't handle them.
    context.ClearError();
}
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim context As HttpContext = HttpContext.Current
Response.Write("<p>HttpContext.Current Example:</p>")

' Add three custom exceptions.
context.AddError(New Exception("New Exception #1"))
context.AddError(New Exception("New Exception #2"))
context.AddError(New Exception("New Exception #3"))

' Capture all the new Exceptions in an array.
Dim errs As Exception() = context.AllErrors

For Each ex As Exception In errs
Response.Write("<p>" & Server.HtmlEncode(ex.ToString()) & "</p>")
Next

' Clear the exceptions so ASP.NET won't handle them.
context.ClearError()
End Sub

備註

這個屬性是 類別的 HttpContext 靜態屬性。 屬性會 HttpContext 儲存套用至目前要求的實例。 這個實例的屬性是 類別的非靜態屬性 HttpContext

您也可以使用 Page.Context 屬性來存取 HttpContext 目前 HTTP 要求的 物件。

適用於

另請參閱