Page.Context Propriété

Définition

Obtient l'objet HttpContext associé à la 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

Valeur de propriété

HttpContext

Objet HttpContext qui contient des informations associées à la page active.

Exemples

L’exemple de code suivant utilise la Context propriété pour accéder HttpContext.AddError aux méthodes et aux méthodes et HttpContext.ClearError à la HttpContext.AllErrors propriété. L’exemple crée trois exceptions personnalisées à l’aide de la méthode et utilise la AddError AllErrors propriété pour charger ces exceptions dans un tableau. Il écrit ensuite le tableau dans la page contenante et utilise la ClearError méthode pour effacer toutes les erreurs de la Context propriété.

     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

Remarques

Cette propriété fournit un accès programmatique au contexte dans lequel la page s’exécute, notamment des informations sur la demande, la réponse, la session et l’application.

S’applique à

Voir aussi