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 HttpModulesSection class and related types.
An HTTP module is a type that implements the IHttpModule interface and provides request processing by handing application events. For more information about HTTP modules, see Introduction to HTTP Modules.
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.
Note |
---|
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 (Example 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 How to: Create and Configure Virtual Directories in IIS.
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.
Note 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 How to: Create Custom HTTP Modules. For information about compilation options, see ASP.NET Compilation Model.
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 (Example Configuration Code for an 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 the System.Web.HttpResponse.Output property 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
See Also
Reference
httpModules Element (ASP.NET Settings Schema)
HttpModuleAction
HttpModuleActionCollection
HttpModulesSection