How to: Create and Configure an HTTP Module
This topic describes how you can programmatically configure the httpModules element of a Web-application configuration by using the class and related types.
An HTTP module is a type that implements theinterface and provides request processing by handing application events. For more information about HTTP modules, see .
The following table lists the accompanying topics, which include the code for an example HTTP module and an example configuration that programmatically adds and removes the HTTP module from an ASP.NET Web application. The HTTP module in these examples is a simple library application called RequestTimeIntervalModule.dll. Members of the RequestTimeIntervalModule class calculate the time interval between the beginning and the end of the Web request, obtain the principal's ID, and finally display the information on any Web page in the application.
Illustrates an HTTP module implementation.
Modifies a Web.config file and adds and removes the HTTP module from a Web application.
Using the Example HTTP Module
The following preparatory steps are required to use the accompanying code examples.
You must run this example in an intranet environment where you can use Windows authentication to authenticate the users. By default, only administrators have read/write access to a Web.config file.
To create the HTTP module
Compile the code for the HTTP module () as a class library called RequestTimeIntervalModule. This produces an assembly DLL.
Create a Web application named Test. You can create the application in IIS as a virtual directory named Test. If you need information about creating and configuring an IIS virtual directory, see.
Create a Bin directory directly under the root directory of your Web application (which is also called the Web-application root).
In the Bin directory, copy the RequestTimeIntervalModule.dll file.
Alternatively, you can use the ASP.NET dynamic compilation feature to test your HTTP module code without pre-compiling an assembly DLL. ASP.NET dynamically compiles source code files that are placed in the App_Code directory directly under the root directory of your Web site. Therefore, classes in source files in the App_Code directory can be accessed from pages without being pre-compiled into assemblies. The App_Code directory is a new feature that is available in ASP.NET version 2.0. Storing your HTTP module code in the App_Code directory allows you to easily test the module without pre-compiling the code. For an example that uses this kind of implementation, see. For information about compilation options, see .
Configuring the Example HTTP Module
This code example uses the HttpModulesSection class and related types to configure the application.
To configure the HTTP module
Compile the code for configuring the HTTP module () as a console application.
From a command-line, call your console application. If there is no existing setting for the RequestTimeIntervalModule HTTP module, one is created. If there is no existing Web.config file in your ASP.NET application, one is created with the new setting. The setting might look like the following httpModules element, as a child element of the system.web element, in your Web.config file.
<httpModules> <add name="RequestTimeIntervalModule" type="Samples.Aspnet.HttpModuleExamples.RequestTimeIntervalModule"> </add> </httpModules>
Testing the Example HTTP Module
HTTP modules enable you to examine the outbound response and modify it. The RequestTimeIntervalModule HTTP module uses theproperty to append some text to the response body.
To test the HTTP module
In the root directory, create a blank ASP.NET page called Default.aspx.
Open a Web browser and request the Default.aspx page from the application called Test, which you created earlier.
The RequestTimeIntervalModule HTTP module appends the following text to any requested page in your ASP.NET application:
Current user: <user name>
Time span between begin-request and end-request events: <timespan>
Your friendly HttpModule: RequestTimeIntervalModule