Skip to content

Middleware

Context: Middleware components are assembled into a pipeline to handle requests and responses. Common in ASP.NET Core.

public delegate Task RequestDelegate(HttpContext context);
public class MiddlewarePipeline
{
private readonly List<Func<RequestDelegate, RequestDelegate>> _components = new();
public void Use(Func<RequestDelegate, RequestDelegate> middleware)
{
_components.Add(middleware);
}
public RequestDelegate Build()
{
RequestDelegate app = context => Task.CompletedTask;
for (int i = _components.Count - 1; i >= 0; i--)
app = _components[i](app);
return app;
}
}
// Example middleware
app.Use(async (context, next) =>
{
Console.WriteLine("Before");
await next();
Console.WriteLine("After");
});

HTTP request pipeline: ASP.NET Core uses middleware for authentication, logging, static files, CORS, exception handling, and MVC. Each middleware can short‑circuit the pipeline or pass to the next.

Example: ASP.NET Core middleware documentation shows built‑in middleware like UseAuthentication, UseAuthorization, UseStaticFiles. Custom middleware for request logging, API rate limiting, or adding correlation IDs.