Page.Context 属性

定义

获取与该页关联的 HttpContext 对象。Gets the HttpContext object associated with the page.

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 对象,它包含与当前页关联的信息。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 方法清除属性中的所有错误 ContextIt 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.

适用于

另请参阅