Freigeben über


HttpApplication-Klasse

Definiert die Methoden, Eigenschaften und Ereignisse, die allen Anwendungsobjekten in einer ASP.NET-Anwendung gemeinsam sind. Diese Klasse ist die Basisklasse für Anwendungen, die vom Benutzer in der Datei Global.asax definiert wurden.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

Syntax

'Declaration
Public Class HttpApplication
    Implements IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
'Usage
Dim instance As HttpApplication
public class HttpApplication : IHttpAsyncHandler, IHttpHandler, IComponent, 
    IDisposable
public ref class HttpApplication : IHttpAsyncHandler, IHttpHandler, IComponent, 
    IDisposable
public class HttpApplication implements IHttpAsyncHandler, IHttpHandler, 
    IComponent, IDisposable
public class HttpApplication implements IHttpAsyncHandler, IHttpHandler, 
    IComponent, IDisposable

Hinweise

Instanzen der HttpApplication-Klasse werden in der ASP.NET-Infrastruktur und nicht direkt vom Benutzer erstellt. Eine einzelne Instanz der HttpApplication-Klasse wird während ihrer Lebensdauer zum Verarbeiten vieler Anforderungen verwendet, wobei sie aber zu einem bestimmten Zeitpunkt nur jeweils eine Anforderung verarbeiten kann. Daher können Membervariablen zum Speichern von Daten pro Anforderung verwendet werden.

Eine Anwendung führt Ereignisse aus, die von Modulen oder von Benutzercode behandelt werden, der in der Datei Global.asax in der folgenden Reihenfolge definiert ist:

  1. BeginRequest

  2. AuthenticateRequest

  3. PostAuthenticateRequest

  4. AuthorizeRequest

  5. PostAuthorizeRequest

  6. ResolveRequestCache

  7. PostResolveRequestCache

    Nach dem PostResolveRequestCache-Ereignis und vor dem PostMapRequestHandler-Ereignis wird ein Ereignishandler (eine Seite entsprechend dem angeforderten URL) erstellt.

  8. PostMapRequestHandler

  9. AcquireRequestState

  10. PostAcquireRequestState

  11. PreRequestHandlerExecute

    Der Ereignishandler wird ausgeführt.

  12. PostRequestHandlerExecute

  13. ReleaseRequestState

  14. PostReleaseRequestState

    Nach dem PostReleaseRequestState-Ereignis wird die Ausgabe ggf. von Antwortfiltern gefiltert.

  15. UpdateRequestCache

  16. PostUpdateRequestCache

  17. EndRequest

Beispiel

In den folgenden zwei Beispielen wird veranschaulicht, wie die HttpApplication-Klasse und ihre Ereignisse verwendet werden. Im Codebeispiel wird veranschaulicht, wie ein benutzerdefiniertes HTTP-Modul erstellt und ein Ereignis damit verbunden wird. Im zweiten Beispiel wird das Ändern der Datei Web.config beschrieben.

Im folgenden Codebeispiel wird veranschaulicht, wie ein benutzerdefiniertes HTTP-Modul erstellt und das AcquireRequestState-Ereignis mit dem HTTP-Modul verbunden wird. HTTP-Module fangen alle Anforderungen an Webanwendungsressourcen ab und ermöglichen dadurch das Filtern von Clientanforderungen. Alle HTTP-Module, die ein HttpApplication-Ereignis abonnieren, müssen die IHttpModule-Schnittstelle implementieren.

Imports System
Imports System.Web

Namespace Samples.AspNet.VB
    Public Class CustomHTTPModule
        Implements IHttpModule

        Public Sub New()

            ' Class constructor.

        End Sub


        ' Classes that inherit IHttpModule 
        ' must implement the Init and Dispose methods.
        Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init

            AddHandler app.AcquireRequestState, AddressOf app_AcquireRequestState
            AddHandler app.PostAcquireRequestState, AddressOf app_PostAcquireRequestState

        End Sub


        Public Sub Dispose() Implements IHttpModule.Dispose

            ' Add code to clean up the
            ' instance variables of a module.

        End Sub


        ' Define a custom AcquireRequestState event handler.
        Public Sub app_AcquireRequestState(ByVal o As Object, ByVal ea As EventArgs)

            Dim httpApp As HttpApplication = CType(o, HttpApplication)
            Dim ctx As HttpContext = HttpContext.Current
            ctx.Response.Write(" Executing AcquireRequestState ")

        End Sub

        ' Define a custom PostAcquireRequestState event handler.
        Public Sub app_PostAcquireRequestState(ByVal o As Object, ByVal ea As EventArgs)

            Dim httpApp As HttpApplication = CType(o, HttpApplication)
            Dim ctx As HttpContext = HttpContext.Current
            ctx.Response.Write(" Executing PostAcquireRequestState ")

        End Sub

    End Class

End Namespace
using System;
using System.Web;

namespace Samples.AspNet.CS
{
    public class CustomHTTPModule : IHttpModule
    {
        public CustomHTTPModule()
        {
            // Class constructor.
        }

        // Classes that inherit IHttpModule 
        // must implement the Init and Dispose methods.
        public void Init(HttpApplication app)
        {

            app.AcquireRequestState += new EventHandler(app_AcquireRequestState);
            app.PostAcquireRequestState += new EventHandler(app_PostAcquireRequestState);
        }

        public void Dispose()
        {
            // Add code to clean up the
            // instance variables of a module.
        }

        // Define a custom AcquireRequestState event handler.
        public void app_AcquireRequestState(object o, EventArgs ea)
        {
            HttpApplication httpApp = (HttpApplication)o;
            HttpContext ctx = HttpContext.Current;
            ctx.Response.Write(" Executing AcquireRequestState ");
        }

        // Define a custom PostAcquireRequestState event handler.
        public void app_PostAcquireRequestState(object o, EventArgs ea)
        {
            HttpApplication httpApp = (HttpApplication)o;
            HttpContext ctx = HttpContext.Current;
            ctx.Response.Write(" Executing PostAcquireRequestState ");
        }

    }
}

Bevor ein Ereignis in einem benutzerdefinierten HTTP-Modul eintreten kann, müssen Sie die Konfigurationseinstellungen in der Datei Web.config ändern, um ASP.NET über das HTTP-Modul zu benachrichtigen. Im folgenden Codebeispiel wird die korrekte Konfigurationseinstellung innerhalb des httpModules-Abschnitts der Datei Web.config veranschaulicht.

<configuration>
  <system.web>
    <httpModules>
      <add type="Samples.AspNet.CS.CustomHTTPModule"
           name="CustomHttpModule" />
      </httpModules>
  </system.web>
</configuration>
<configuration>
  <system.web>
    <httpModules>
      <add type="Samples.AspNet.VB.CustomHTTPModule"
           name="CustomHttpModule" />
      </httpModules>
  </system.web>
</configuration>

.NET Framework-Sicherheit

Vererbungshierarchie

System.Object
  System.Web.HttpApplication

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

HttpApplication-Member
System.Web-Namespace
IHttpHandlerFactory
IHttpHandler
IHttpModule

Weitere Ressourcen

httpHandlers-Element (ASP.NET-Einstellungsschema)
httpModules-Element (ASP.NET-Einstellungsschema)