Page.Context Page.Context Page.Context Page.Context Property

定義

取得與網頁關聯的 HttpContext 物件。Gets the HttpContext object associated with the page.

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

屬性值

HttpContext 物件,包含與目前網頁相關聯的資訊。An HttpContext object that contains information associated with the current page.

範例

下列程式碼範例會使用Context屬性來存取HttpContext.AddErrorHttpContext.ClearError方法和HttpContext.AllErrors屬性。The following code example uses the Context 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.

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

備註

這個屬性會提供以程式設計方式存取 頁面中執行,包括要求、 回應、 工作階段和應用程式的相關資訊的內容。This property provides programmatic access to the context the page runs in, including information about the request, response, session, and application.

適用於

另請參閱