Share via


HttpContext.Current Eigenschaft

Definition

Ruft das HttpContext-Objekt für die aktuelle HTTP-Anforderung ab oder legt dieses fest.

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

Eigenschaftswert

HttpContext

Die HttpContext-Instanz für die aktuelle HTTP-Anforderung.

Beispiele

Im folgenden Codebeispiel wird die Eigenschaft verwendet, um auf die Current und die HttpContext.AddError HttpContext.ClearError Methoden und die HttpContext.AllErrors Eigenschaft zuzugreifen. Im Beispiel werden drei benutzerdefinierte Ausnahmen mithilfe der AddError Methode erstellt und die AllErrors Eigenschaft verwendet, um diese Ausnahmen in ein Array zu laden. Anschließend wird das Array auf die ClearError enthaltende Seite geschrieben und die Methode verwendet, um alle Fehler aus der Context Eigenschaft zu löschen.

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

Hinweise

Diese Eigenschaft ist eine statische Eigenschaft der HttpContext Klasse. Die Eigenschaft speichert die Instanz, die für die HttpContext aktuelle Anforderung gilt. Die Eigenschaften dieser Instanz sind die nicht statischen Eigenschaften der HttpContext Klasse.

Sie können auch die Eigenschaft verwenden, um auf das Page.Context Objekt für die HttpContext aktuelle HTTP-Anforderung zuzugreifen.

Gilt für

Siehe auch