Overview
ASP.NET Core middleware are software components assembled into an application pipeline to handle requests and responses. Each component can:
- Perform actions before and after the next component in the pipeline.
- Short‑circuit the pipeline to produce a response.
- Invoke the next component via
await next(context).
Key concepts
| Concept | Description |
|---|---|
| RequestDelegate | Represents the next middleware in the pipeline. |
| HttpContext | Encapsulates all HTTP-specific information about an individual HTTP request. |
| Use/Run | Extension methods on IApplicationBuilder to add middleware. |
| Terminal middleware | Middleware that does not call next(), ending the pipeline. |
Getting started
To add middleware, use the Configure method in Startup.cs (or Program.cs in minimal hosting):
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
Read the Creating Middleware page for a step‑by‑step guide on building custom middleware.