Freigeben über


Beispiel für ein benutzerdefiniertes HttpModule

Das folgende benutzerdefinierte Modul gibt einfach eine Webseitenmeldung am Anfang jeder HTTP-Anforderung zurück und eine weitere, sobald die Anforderung verarbeitet wurde. Die unten angegebene Init-Funktion registriert Ereignishandler für zwei HttpApplication-Ereignisse (BeginRequest und EndRequest). Jeder Handler wird als private Methode des Moduls geschrieben. Wenn die registrierten Ereignisse ausgelöst werden, ruft ASP.NET die entsprechende Handlermethode auf, die eine Webseite schreibt und dann beendet wird.

using System;
using System.Web; 
using System.Collections;

public class HelloWorldModule : IHttpModule {
    public String ModuleName { 
        get { return "HelloWorldModule"; } 
    }    
    
    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application) {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += (new EventHandler(this.Application_EndRequest));
    }
    
    // Your BeginRequest event handler.
    private void Application_BeginRequest(Object source, EventArgs e) {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        context.Response.Write("<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>");
    }
    
    // Your EndRequest event handler.
    private void Application_EndRequest(Object source, EventArgs e) {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>");
    }        
    
    public void Dispose() 
    {
    }
}

[Visual Basic]
Imports System
Imports System.Web
Imports System.Collections

Public Class HelloWorldModule
    Implements IHttpModule

    Public ReadOnly Property ModuleName() As [String]
        Get
            Return "HelloWorldModule"
        End Get
    End Property

    ' In the Init function, register for HttpApplication 
    ' events by adding your handlers.
    Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
        AddHandler application.BeginRequest, AddressOf Me.Application_BeginRequest
        AddHandler application.EndRequest, AddressOf Me.Application_EndRequest
    End Sub

    ' Your BeginRequest event handler.
    Private Sub Application_BeginRequest(ByVal [source] As [Object], ByVal e As EventArgs)
        Dim application As HttpApplication = CType([source], HttpApplication)
        Dim context As HttpContext = application.Context
        context.Response.Write("<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>")
    End Sub

    ' Your EndRequest event handler.
    Private Sub Application_EndRequest(ByVal [source] As [Object], ByVal e As EventArgs)
        Dim application As HttpApplication = CType([source], HttpApplication)
        Dim context As HttpContext = application.Context
        context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>")
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub
End Class

Registrieren Sie das Modul folgendermaßen:

<configuration>
    <system.web>
        <httpModules>
            <!-- <add name="HelloWorldModule" 
                      type="HelloWorldModule, HelloWorldModule" /> -->
        </httpModules>
    </system.web>
</configuration>

Siehe auch

HTTP-Laufzeitunterstützung | HttpModules | Behandeln von öffentlichen Ereignissen | Behandeln und Auslösen von Ereignissen