共用方式為


Page.Context 屬性

定義

取得與網頁關聯的 HttpContext 物件。

protected:
 virtual property System::Web::HttpContext ^ Context { System::Web::HttpContext ^ get(); };
protected public:
 virtual property System::Web::HttpContext ^ Context { System::Web::HttpContext ^ get(); };
protected override System.Web.HttpContext Context { get; }
protected internal override System.Web.HttpContext Context { get; }
member this.Context : System.Web.HttpContext
Protected Overrides ReadOnly Property Context As HttpContext
Protected Friend Overrides ReadOnly Property Context As HttpContext

屬性值

HttpContext

HttpContext 物件,包含與目前網頁相關聯的資訊。

範例

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

     void Page_Load(Object sender,EventArgs e) 
     {
        Response.Write("<h3>Page.Context Example:</h3>");

        // Add three custom exceptions.
        Context.AddError(new Exception(
            "<h3 style='color: red'>New Exception #1.</h3>"));
        Context.AddError(new Exception(
            "<h3 style='color: red'>New Exception #2.</h3>"));
        Context.AddError(new Exception(
            "<h3 style='color: red'>New Exception #3.</h3>"));

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

        foreach (Exception ex in errs)
        {
           Response.Write("<p style='text-align:center; ");
           Response.Write("font-weight:bold'>");
           Response.Write(Server.HtmlEncode(ex.ToString()) + "</p>"); 
        }

        // Clear the exceptions so ASP.NET won't handle them.
        Context.ClearError();
     }

     Sub Page_Load(Sender As Object, e As EventArgs ) 
     
        Response.Write("<h3>Page.Context Example:</h3>")
           
        ' Add three custom exceptions.
       Context.AddError(New Exception( _
           "<h3 style=""color: red"">New Exception #1.</h3>"))
       Context.AddError(New Exception( _
           "<h3 style=""color: red"">New Exception #2.</h3>"))
       Context.AddError(New Exception( _
           "<h3 style=""color: red"">New Exception #3.</h3>"))

        ' Capture all the new Exceptions in an array.
        Dim errs() As Exception = Context.AllErrors
        Dim ex As Exception
        
        For Each ex In errs
           Response.Write("<p style='text-align:center; font-weight:bold'>")
           Response.Write(Server.HtmlEncode(ex.ToString()) + "</p>")
        Next

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

備註

此屬性提供頁面執行內容的程式設計存取,包括要求、回應、會話和應用程式的相關資訊。

適用於

另請參閱