What is Middleware?
In modern .NET (especially .NET Core), Middleware refers to components that are assembled into an application pipeline to handle requests and responses.
But in .NET Framework (ASP.NET MVC or Web Forms), we don’t have native UseMiddleware()
like in .NET Core.
Instead, HTTP Modules and HTTP Handlers provide similar functionality.
Why Do You Need Middleware?
Middleware (or its equivalent in .NET Framework) helps you:
Add cross-cutting concerns like logging, authentication, or caching
Centralize request and response processing
Avoid duplicating logic across controllers
Implementing a Custom Middleware with HttpModule
Here’s how you create and register a custom middleware in .NET Framework:
🔧 Step 1: Create the HttpModule Class
https://dotnetfullstackdev.medium.com/
using System;
using System.Web;
public class RequestLoggerModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (src, args) =>
{
var request = ((HttpApplication)src).Context.Request;
// Example: Logging incoming request
Console.WriteLine($"Request URL: {request.Url}");
};
context.EndRequest += (src, args) =>
{
var response = ((HttpApplication)src).Context.Response;
Console.WriteLine($"Response Status Code: {response.StatusCode}");
};
}
public void Dispose()
{
// Cleanup if needed
}
}
🔧 Step 2: Register the Module in web.config
<configuration>
<system.webServer>
<modules>
<add name="RequestLoggerModule" type="YourNamespace.RequestLoggerModule" />
</modules>
</system.webServer>
</configuration>
✅ This will now log every request and response — just like middleware in .NET Core!
Bonus: Custom Response Middleware (e.g., for adding custom headers)
public class CustomHeaderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.EndRequest += (sender, e) =>
{
HttpContext.Current.Response.Headers.Add("X-Developer", "YourName");
};
}
public void Dispose() { }
}
Mimicking Middleware Chaining
Middleware chaining isn’t native in .NET Framework like it is in .NET Core (app.Use() → next()
), but you can:
Use multiple
HttpModules
and control their execution orderCombine logic inside a single
HttpModule
and call sub-methods like a chain
Sample Use Case: Maintenance Mode Middleware
public class MaintenanceModeModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, args) =>
{
if (DateTime.Now.Hour >= 2 && DateTime.Now.Hour < 3)
{
HttpContext.Current.Response.StatusCode = 503;
HttpContext.Current.Response.StatusDescription = "Service Unavailable";
HttpContext.Current.Response.End();
}
};
}
public void Dispose() { }
}
Summary
✅ While .NET Framework doesn’t have modern middleware like .NET Core, you can still:
Use
HttpModules
andHttpHandlers
Create centralized logic
Register them in
web.config
Handle requests and responses before/after MVC routing
When to Migrate to .NET Core Middleware?
If you want:
Better pipeline control
Use
,UseWhen
,Map
, and other expressive middleware featuresNative DI and pipeline chaining
Then .NET Core is worth adopting.