通过


HttpContext.Current 属性

定义

获取或设置 HttpContext 当前 HTTP 请求的对象。

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; }
static member Current : System.Web.HttpContext with get, set
Public Shared Property Current As HttpContext

属性值

HttpContext当前 HTTP 请求的实例。

示例

下面的代码示例使用 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 请求的对象。

适用于

另请参阅