Microsoft KB Archive/308000

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

Article ID: 308000

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q308000



For a Microsoft Visual C# .NET version of this article, see 307996.

IMPORTANT: This article assumes that you are familiar with the following topic:

Visual Basic .NET syntax

IN THIS TASK
SUMMARY
 * Implement the Module
 * Deploy the Module
 * Configure the System
 * Test the Module

REFERENCES



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.

back to the top

Implement the Module
 In Visual Studio .NET, create a new Visual Basic .NET Class Library project named MyModule. Set a reference to the System.Web.dll assembly.  Import the System.Web namespace as follows: Imports System.Web   Implement the IHttpModule interface. Your class definition should appear as follows: Public Class SyncModule Implements IHttpModule  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.</li> AuthorizeRequest: Call this event by a security module when the request needs to be authorized. Call this event after authentication.</li> BeginRequest: Call this event to notify a module that new request is beginning.</li> Disposed: Call this event to notify the module that the application is ending for some reason. Allows the module to perform internal cleanup.</li> EndRequest: Call this event to notify the module that the request is ending.</li> Error: Call this event to notify the module of an error that occurs during request processing.</li> PostRequestHandlerExecute: Call this event to notify the module that the handler has finished processing the request.</li> PreRequestHandlerExecute: Call this event to notify the module that the handler for the request is about to be called.</li> PreSendRequestContent: Call this event to notify the module that content is about to be sent to the client.</li> PreSendRequestHeaders: Call this event to notify the module that the HTTP headers are about to be sent to the client.</li> ReleaseRequestState: Call this event to allow the module to release state because the handler has finished processing the request.</li> 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.</li> UpdateRequestCache: Call this event after a response from the handler. Caching modules should update their cache with the response.</li></ul>

This sample uses the BeginRequest event.</li>  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 </li>  Create a delegate for an event as follows: Public Delegate Sub MyEventHandler(ByVal s As Object, ByVal e As EventArgs) </li>  Define a public local variable of the type MyEventHandler to hold a reference to the event: Public Event MyEvent As MyEventHandler </li> <li> 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(&quot;Hello from OnBeginRequest in custom module. &quot;) RaiseEvent MyEvent(Me, Nothing) End Sub </li> <li>Compile the project.</li></ol>

back to the top

Deploy the Module
<ol> <li>Create a new directory under C:\Inetpub\Wwwroot named Module.</li> <li>Create a subdirectory named Bin in the newly created Module directory. The resultant path is C:\Inetpub\Wwwroot\Module\Bin.</li> <li>Copy MyModule.dll from your project's Bin\Debug directory to the C:\Inetpub\Wwwroot\Module\Bin directory.</li> <li>Follow these steps to mark the new Module directory as a Web application: <ol style="list-style-type: lower-alpha;"> <li>Open Internet Services Manager.</li> <li>Right-click the Module directory, and then click Properties.</li> <li>On the Directory tab, click Create.</li> <li>Click OK to close the Module Properties dialog box.</li></ol> </li></ol>

back to the top

Configure the System
<ol> <li>In the C:\Inetpub\Wwwroot\Module directory, create a new file named Web.config.</li> <li> Paste the following text into Web.config: <system.web> <httpModules> <add name=&quot;MyModule&quot; type=&quot;MyModule.SyncModule, MyModule&quot; /> </httpModules> </system.web> </li></ol>

back to the top

Test the Module
<ol> <li>In the C:\Inetpub\Wwwroot\Module directory, create a new .aspx file named Test.aspx.</li> <li> Paste the following text into Test.aspx: <%@Page Language=&quot;VB&quot;%> <% Response.Write(&quot;Hello from Test.aspx. &quot;) %> </li> <li>In the C:\Inetpub\Wwwroot\Module directory, create a Global.asax file.</li> <li> Paste the following code in Global.asax: <%@ Import Namespace=&quot;MyModule&quot; %>

<script language=&quot;VB&quot; runat=server > Public Sub MyModule_OnMyEvent(src As Object, e As EventArgs) Context.Response.Write(&quot;Hello from MyModule_OnMyEvent called in Global.asax. &quot;) End Sub </li> <li> 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. </li></ol>

back to the top

<div class="references_section">