HttpContext.Current Propriedade

Definição

Obtém ou define o objeto HttpContext para a atual solicitação HTTP.Gets or sets the HttpContext object for the current HTTP request.

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

Valor da propriedade

HttpContext

A instância HttpContext da atual solicitação HTTP.The HttpContext instance for the current HTTP request.

Exemplos

O exemplo de código a seguir usa a Current propriedade para acessar os HttpContext.AddError HttpContext.ClearError métodos e e a HttpContext.AllErrors propriedade.The following code example uses the Current property to access the HttpContext.AddError and HttpContext.ClearError methods and the HttpContext.AllErrors property. O exemplo cria três exceções personalizadas usando o AddError método e usa a AllErrors propriedade para carregar essas exceções em uma matriz.The example creates three custom exceptions using the AddError method and uses the AllErrors property to load these exceptions to an array. Em seguida, ele grava a matriz na página que a contém e usa o ClearError método para limpar todos os erros da Context propriedade.It then writes the array to the containing page and uses the ClearError method to clear all the errors from the Context property.

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

Comentários

Essa propriedade é uma propriedade estática da HttpContext classe.This property is a static property of the HttpContext class. A propriedade armazena a HttpContext instância que se aplica à solicitação atual.The property stores the HttpContext instance that applies to the current request. As propriedades dessa instância são as propriedades não estáticas da HttpContext classe.The properties of this instance are the non-static properties of the HttpContext class.

Você também pode usar a Page.Context propriedade para acessar o HttpContext objeto para a solicitação HTTP atual.You can also use the Page.Context property to access the HttpContext object for the current HTTP request.

Aplica-se a

Confira também