Bagikan melalui


HttpContext.Current Properti

Definisi

Mendapatkan atau mengatur HttpContext objek untuk permintaan HTTP saat ini.

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

Nilai Properti

Instans HttpContext untuk permintaan HTTP saat ini.

Contoh

Contoh kode berikut menggunakan Current properti untuk mengakses HttpContext.AddError metode dan HttpContext.ClearError dan HttpContext.AllErrors properti . Contoh membuat tiga pengecualian kustom menggunakan AddError metode dan menggunakan AllErrors properti untuk memuat pengecualian ini ke array. Kemudian menulis array ke halaman yang berisi dan menggunakan ClearError metode untuk menghapus semua kesalahan dari Context properti.

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

Keterangan

Properti ini adalah properti statis dari HttpContext kelas . Properti menyimpan instans HttpContext yang berlaku untuk permintaan saat ini. Properti instans ini adalah properti non-statis kelas HttpContext .

Anda juga dapat menggunakan Page.Context properti untuk mengakses HttpContext objek untuk permintaan HTTP saat ini.

Berlaku untuk

Lihat juga