Creating an HTTP module
Programming
Published on March 13, 2025
Table of Contents
Introduction
- Use .NET Framework 4.x (not .NET Core)
- Create as a class library
- Language is C#
Minimum Code to Create an HTTP Module
// CustomHttpModule.cs
using System.Web;
public class CustomHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) =>
{
HttpContext.Current.Response.Write("<p>This is Test</p>");
};
}
public void Dispose() { }
}
Deployment Method
- Place CustomHttpModule.dll in C:\var\testweb\bin
- Add C:\var\testweb as a website in IIS Manager
*Add it as a website, not a virtual directory.
Therefore, either disable the Default Web Site, or if you're using the Default Web Site, create a bin folder under it and place the dll there. - Select the website in IIS Manager and open "Modules" in the Features View
- On the right, select Operations > Add Managed Module > CustomHttpModule
*You cannot perform "Add Managed Module" with a virtual directory.
A web.config file will be generated with the following content:
// web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules>
<add name="CustomHttpModule" type="CustomHttpModule" />
</modules>
</system.webServer>
</configuration>
- Create index.html in C:\var\testweb\
<html>
<head>
<meta charset="UTF-8">
</head>
<body>テスト</body>
</html>
- Restart IIS Launch Command Prompt in administrator mode and execute the following:
iisreset
Operation Check
Access http://localhost/ in your browser
As shown below, <p>This is Test</p>
will be inserted into the original HTML.
Figure 1. Tag added by HTTP module
Result of "View Page Source"
<p>This is Test</p><html>
<head>
<meta charset="UTF-8">
</head>
<body>テスト</body>
</html>
Debugging Log Output and Log Viewing Method
Add Debugging Code
Debug.WriteLine
is the part that outputs debug logs
// CustomHttpModule.cs
using System.Diagnostics;
using System.Web;
public class CustomHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
Debug.WriteLine("CustomHttpModule: Init Start");
context.BeginRequest += (sender, e) =>
{
Debug.WriteLine("CustomHttpModule: BeginRequest Start");
HttpContext.Current.Response.Write("<p>This is Test</p>");
};
}
public void Dispose() { }
}
DebugView
- Download DebugView for Windows (Sysinternals)
- Unzip the file and run Dbgview.exe with administrator privileges (this is the program itself, not an installer)
- Check Capture > Capture Global Win32 (this allows you to view IIS debug logs)
- Access http://localhost/
Logs will be output as shown below: Figure 2. DebugView
If logs are not output
Logs may not be output depending on the user privilege settings of the IIS worker process (w3wp.exe).
In such cases, changing the application pool's ID from ApplicationPoolIdentity
to LocalSystem
increases the likelihood of logs being displayed.
- IIS Configuration Change
- Application Pool Advanced Settings > Process Model > Change Identity to built-in account
LocalSystem
- Restart IIS
- Application Pool Advanced Settings > Process Model > Change Identity to built-in account