HttpContext.Current HttpContext.Current HttpContext.Current HttpContext.Current Property

定義

取得或設定目前 HTTP 要求的 HttpContext 物件。Gets or sets the HttpContext object for the current HTTP request.

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

屬性值

目前 HTTP 要求的 HttpContext 執行個體。The HttpContext instance for the current HTTP request.

範例

下列程式碼範例會使用Current屬性來存取HttpContext.AddErrorHttpContext.ClearError方法和HttpContext.AllErrors屬性。The following code example uses the Current property to access the HttpContext.AddError and HttpContext.ClearError methods and the HttpContext.AllErrors property. 此範例會建立三個使用的自訂例外狀況AddError方法,並使用AllErrors載入陣列,這些例外狀況的屬性。The example creates three custom exceptions using the AddError method and uses the AllErrors property to load these exceptions to an array. 然後將陣列寫入至 [包含] 頁面,並使用ClearError方法,以清除所有的錯誤Context屬性。It then writes the array to the containing page and uses the ClearError method to clear all the errors from the Context property.

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類別。This property is a static property of the HttpContext class. 屬性存放區HttpContext套用至目前要求的執行個體。The property stores the HttpContext instance that applies to the current request. 這個執行個體的屬性是的非靜態屬性HttpContext類別。The properties of this instance are the non-static properties of the HttpContext class.

您也可以使用Page.Context屬性來存取HttpContext目前 HTTP 要求的物件。You can also use the Page.Context property to access the HttpContext object for the current HTTP request.

適用於

另請參閱