Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Create an ASP.NET HTTP Module Using Visual Basic .NET


View products that this article applies to.

Summary

This step-by-step article demonstrates how to use Visual Basic .NET to create a simple, custom HTTP module. This article demonstrates how to create, deploy, and configure the module, as well as how to hook up an event for the module in the Global.asax file.


Implement the Module

  1. In Visual Studio .NET, create a new Visual Basic .NET Class Library project named MyModule.
  2. Set a reference to the System.Web.dll assembly.
  3. Import the System.Web namespace as follows:
    Imports System.Web
    					
  4. Implement the IHttpModule interface. Your class definition should appear as follows:
    Public Class SyncModule
       Implements IHttpModule
    					
  5. Decide to which events you will subscribe. The following list outlines the available events from the HttpApplication object to which you can subscribe:

    • AcquireRequestState: Call this event to allow the module to acquire or create the state (for example, session) for the request.
    • AuthenticateRequest: Call this event when a security module needs to authenticate the user before it processes the request.
    • AuthorizeRequest: Call this event by a security module when the request needs to be authorized. Call this event after authentication.
    • BeginRequest: Call this event to notify a module that new request is beginning.
    • Disposed: Call this event to notify the module that the application is ending for some reason. Allows the module to perform internal cleanup.
    • EndRequest: Call this event to notify the module that the request is ending.
    • Error: Call this event to notify the module of an error that occurs during request processing.
    • PostRequestHandlerExecute: Call this event to notify the module that the handler has finished processing the request.
    • PreRequestHandlerExecute: Call this event to notify the module that the handler for the request is about to be called.
    • PreSendRequestContent: Call this event to notify the module that content is about to be sent to the client.
    • PreSendRequestHeaders: Call this event to notify the module that the HTTP headers are about to be sent to the client.
    • ReleaseRequestState: Call this event to allow the module to release state because the handler has finished processing the request.
    • ResolveRequestCache: Call this event after authentication. Caching modules use this event to determine if the request should be processed by its cache or if a handler should process the request.
    • UpdateRequestCache: Call this event after a response from the handler. Caching modules should update their cache with the response.
    This sample uses the BeginRequest event.
  6. Implement the Init and Dispose methods of the IHttpModule interface as follows:
    Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
       AddHandler app.BeginRequest, AddressOf Me.OnBeginRequest
    End Sub
    
    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub
    					
  7. Create a delegate for an event as follows:
    Public Delegate Sub MyEventHandler(ByVal s As Object, ByVal e As EventArgs)
    					
  8. Define a public local variable of the type MyEventHandler to hold a reference to the event:
    Public Event MyEvent As MyEventHandler
    					
  9. Create the OnBeginRequest method, which hooks up to the BeginRequest event of the HttpApplication object:
    Public Sub OnBeginRequest(ByVal s As Object, ByVal e As EventArgs)
       Dim app As HttpApplication = CType(s, HttpApplication)
       app.Context.Response.Write("Hello from OnBeginRequest in custom module.<br>")
       RaiseEvent MyEvent(Me, Nothing)
    End Sub
    					
  10. Compile the project.

Deploy the Module

  1. Create a new directory under C:\Inetpub\Wwwroot named Module.
  2. Create a subdirectory named Bin in the newly created Module directory. The resultant path is C:\Inetpub\Wwwroot\Module\Bin.
  3. Copy MyModule.dll from your project's Bin\Debug directory to the C:\Inetpub\Wwwroot\Module\Bin directory.
  4. Follow these steps to mark the new Module directory as a Web application:
    1. Open Internet Services Manager.
    2. Right-click the Module directory, and then click Properties.
    3. On the Directory tab, click Create.
    4. Click OK to close the Module Properties dialog box.

Configure the System

  1. In the C:\Inetpub\Wwwroot\Module directory, create a new file named Web.config.
  2. Paste the following text into Web.config:
    <configuration>
       <system.web>
          <httpModules>
             <add name="MyModule" type="MyModule.SyncModule, MyModule" />
          </httpModules>
       </system.web>
    </configuration>
    					

Test the Module

  1. In the C:\Inetpub\Wwwroot\Module directory, create a new .aspx file named Test.aspx.
  2. Paste the following text into Test.aspx:
    <%@Page Language="VB"%>
    <% Response.Write("Hello from Test.aspx.<br>") %>
    					
  3. In the C:\Inetpub\Wwwroot\Module directory, create a Global.asax file.
  4. Paste the following code in Global.asax:
    <%@ Import Namespace="MyModule" %>
    
    <script language="VB" runat=server >
    Public Sub MyModule_OnMyEvent(src As Object, e As EventArgs)	
      Context.Response.Write("Hello from MyModule_OnMyEvent called in Global.asax.<br>")
    End Sub
    </script>
    					
  5. Request the Test.aspx page. You should see the following lines of text:
    Hello from OnBeginRequest in custom module.
    Hello from MyModule_OnMyEvent called in Global.asax.
    Hello from Test.aspx.
    					

↑ Back to the top


References

For additional information on HttpHandlers, click the article number below to view the article in the Microsoft Knowledge Base:
307985 INFO: ASP.NET HTTP Modules and HTTP Handlers Overview

↑ Back to the top


Keywords: KB308000, kbweb, kbhttpruntime, kbhttpmodule, kbhowtomaster, kbconfig

↑ Back to the top

Article Info
Article ID : 308000
Revision : 7
Created on : 7/15/2004
Published on : 7/15/2004
Exists online : False
Views : 547