ASP.NET Core introduces a revolutionary, high-performance, composable request-processing system known as the Middleware Pipeline. Middlewares form the foundation of how requests and responses flow through an ASP.NET Core application.
Whether you're building REST APIs, MVC applications, or microservices, understanding middleware is essential. They control:
Authentication
Routing
Error handling
Logging
Security
Performance
Request/response manipulation
This is not just a basic guide — this is a complete deep dive into how middlewares work internally, how to build them, how to optimize them, and how they fit into enterprise architectures.
What is Middleware in ASP.NET Core?
A middleware is a small, self-contained component that processes HTTP requests. Every incoming request and outgoing response passes through a sequence of middlewares — forming the request pipeline.
Definition
Middleware is a software component that is assembled into an application pipeline to handle requests and responses. Each middleware either processes the request or passes it to the next middleware.
The Middleware Pipeline — How It Really Works
Here is the conceptual flow of how an API request travels through middlewares:
INCOMING REQUEST
↓
┌───────────────────────────┐
│ Middleware 1 (Logging) │
└───────────────────────────┘
↓
┌───────────────────────────┐
│ Middleware 2 (Auth) │
└───────────────────────────┘
↓
┌───────────────────────────┐
│ Middleware 3 (Routing) │
└───────────────────────────┘
↓
Endpoint (Controller)
↓
┌───────────────────────────┐
│ Middleware 3 (Routing) │
└───────────────────────────┘
↑
┌───────────────────────────┐
│ Middleware 2 (Auth) │
└───────────────────────────┘
↑
┌───────────────────────────┐
│ Middleware 1 (Logging) │
└───────────────────────────┘
↑
OUTGOING RESPONSEThis shows a bidirectional flow — most middlewares execute twice:
Once the request enters
Once, while the response leaves
Why Middlewares?
Before ASP.NET Core, developers relied on:
HTTP modules
HTTP handlers
Global.asax
Web.config pipeline
These were heavyweight, inflexible, difficult to customize, and not cross-platform.
Middlewares solve those issues:
Extremely lightweight
Easy to compose
Modular
Cross-platform
Faster due to Kestrel
Developer-friendly
Fully configurable pipeline
Creating Middleware
A middleware must follow one rule:
It must accept a RequestDelegate and return a Task.
Full Custom Middleware Example
public class LoggingMiddleware
{
private readonly RequestDelegate next;
public LoggingMiddleware(RequestDelegate next)
{
next = next;
}
public async Task InvokeAsync(HttpContext context)
{
Console.WriteLine($"Incoming Request: {context.Request.Path}");
await next(context);
Console.WriteLine($"Outgoing Response: {context.Response.StatusCode}");
}
}Register This in the Program.cs
app.UseMiddleware<LoggingMiddleware>();Use(), Run(), Map()
These three functions define the pipeline structure.
app.Use() — Middle layer (Can Continue Pipeline)
app.Use(async (context, next) =>
{
Console.WriteLine("Before");
await next.Invoke();
Console.WriteLine("After");
});Before logic
Calls next()
After logic
Most middlewares use Use().
app.Run() — Terminal Middleware (Stops Pipeline)
app.Run(async context =>
{
await context.Response.WriteAsync("End of Pipeline");
});Does not call next()
Ends pipeline immediately
Often used for:
Custom 404 handler
Maintenance mode
app.Map() — Branching Middleware
app.Map("/admin", adminApp =>
{
adminApp.Run(async context =>
{
await context.Response.WriteAsync("Admin Area");
});
});Path-based branching
Used for versioning, API partitioning
Order of Middlewares
Middleware order decides security, routing, and performance.
Correct order in real projects:

Join the conversation! Your thoughts help the community grow.