The Middleware Pipeline is one of the most important concepts in ASP.NET Core. It defines how HTTP requests and responses are handled inside an application. Every request flows through a sequence of components called middleware, and each middleware can inspect, modify, or terminate the request before passing it to the next component in the pipeline.
Understanding middleware is essential for building secure, scalable, and high-performance web applications. Features such as authentication, logging, error handling, routing, and compression are all implemented using middleware.

What is Middleware?
Middleware is a software component that:
Receives an HTTP request
Can modify the request or response
Can pass control to the next middleware
Can stop the pipeline completely
Each middleware:
Executes code before calling the next middleware.
Calls the
nextdelegate (optional).Executes code after the next middleware finishes.
How the Middleware Pipeline Works
When a request arrives:
It enters the first middleware.
The middleware decides to:
Process the request and pass it further, OR
Short-circuit the request and return a response directly.
The response travels backward through the middleware chain.
This forms a pipeline or chain of execution.
Basic Pipeline Example
In Program.cs:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Use(async (context, next) =>
{
Console.WriteLine("Middleware 1: Before");
await next();
Console.WriteLine("Middleware 1: After");
});
app.Use(async (context, next) =>
{
Console.WriteLine("Middleware 2: Before");
await next();
Console.WriteLine("Middleware 2: After");
});
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from endpoint");
});
app.Run();
Output Order
Middleware 1: Before
Middleware 2: Before
Hello from endpoint
Middleware 2: After
Middleware 1: AfterThis shows request going forward and response coming backward.
Types of Middleware
There are three main types:
1. Built-in Middleware
ASP.NET Core includes many built-in middlewares:
UseAuthentication()UseAuthorization()UseRouting()UseHttpsRedirection()UseStaticFiles()UseCors()UseResponseCompression()
Example
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseRouting();
2. Inline (Anonymous) Middleware
Used directly with app.Use():
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Before Next\n");
await next();
await context.Response.WriteAsync("After Next\n");
});
Useful for logging, validation, and debugging.
3. Custom Middleware Class
Create a middleware class:
public class RequestLoggingMiddleware
{
private readonly RequestDelegate _next;
public RequestLoggingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
Console.WriteLine($"Request: {context.Request.Path}");
await _next(context);
}
}
Register it:
app.UseMiddleware<RequestLoggingMiddleware>();
Use, Run, and Map
ASP.NET Core provides three ways to add middleware.
Use()
Calls the next middleware:
app.Use(async (context, next) =>
{
Console.WriteLine("Before");
await next();
Console.WriteLine("After");
});
Run()
Terminates pipeline and does not call next:

Join the conversation! Your thoughts help the community grow.