How to: Create Custom HTTP Modules

The custom HTTP module described in this topic illustrates the basic functionality of an HTTP module. The module is called in response to two events: the BeginRequest event and the EndRequest event. This enables the module to run before and after a page request is processed. In this case, the module adds a message to the requested ASP.NET Web page at the beginning of any HTTP request and another message after the request has been processed.

NoteNote

The BeginRequest and EndRequest events are only two of the events that occur during page processing. For more information about the events raised while processing a page, see Server Event Handling in ASP.NET Web Pages.

Each handler is written as a private method of the module. When the registered events are raised, ASP.NET calls the appropriate handler method in the module, which writes information to the ASP.NET Web page.

To create a custom HTTP module class

  1. If your Web site does not already have an App_Code folder, create one under the root of the site.

  2. Create a class file named HelloWorldModule.vb (for Visual Basic) or HelloWorldModule.cs (for C#) in the App_Code directory.

NoteNote

Alternatively, you can compile the HelloWorldModule class into a library and place the resulting .dll file in the Web application's Bin directory.

  1. Add the following code to your class file:

    Imports Microsoft.VisualBasic
    
    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
    
        Private Sub Application_BeginRequest(ByVal source As Object, _
                ByVal e As EventArgs)
        ' Create HttpApplication and HttpContext objects to access
        ' request and response properties.
            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
    
        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
    
    public class HelloWorldModule : IHttpModule
    {
        public HelloWorldModule()
        {
        }
    
        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));
        }
    
        private void Application_BeginRequest(Object source, 
             EventArgs e)
        {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write("<h1><font color=red>
                HelloWorldModule: Beginning of Request
                </font></h1><hr>");
        }
    
        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()
        {
        }
    }
    

Registering the HTTP Module

When you have finished creating the HelloWorldModule class, you can register the module by creating an entry in the Web.config file.

To register the module in the Web.config file

  1. If your Web site does not already have a Web.config file, create one under the root of the site.

  2. Add the following highlighted code to the Web.config file:

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

    The code registers the module with the class name and the module name of HelloWorldModule.

Testing the Custom HTTP Module

After you have created and registered your custom HTTP module you can test it.

To test the custom HTTP module

  1. Create a Default.aspx page in your application.

  2. Request the Default.aspx page in a browser.

    The HTTP module appends a string to the beginning and end of the response. The module will automatically run on any request to a file whose extension is assigned to ASP.NET. For more information, see Introduction to HTTP Modules.

See Also

Concepts

Introduction to HTTP Modules
ASP.NET Application Life Cycle Overview