ASP.NET Core middleware runs as an ordered pipeline built with Use, Map and Run
Answer: The ASP.NET Core request pipeline is a sequence of request delegates called one after the other. Each middleware chooses whether to pass the request to the next one, and can work before and after it. Build the pipeline with the Use, Map and Run extension methods.
Example
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Use(async (context, next) =>
{
// before
await next.Invoke(context);
// after (do not write to the response here)
});
app.Map("/status", b => b.Run(ctx => ctx.Response.WriteAsync("ok")));
app.Run(async context => await context.Response.WriteAsync("Hello world!"));
app.Run();
Rules
- In
Use, thenextparameter represents the next delegate in the pipeline. - A middleware that short-circuits is called a terminal middleware, because it stops further middleware from processing the request.
Rundelegates are terminal. - Middleware is invoked on requests in the order it is added, and in the reverse order for the response. Order matters for security, performance and functionality.
- Don't call
next.Invokeafter the response has been sent to the client. ChangingHttpResponseafter the response has started throws an exception, for example when setting headers and a status code.
Branching
Map branches the pipeline by request path. UseWhen branches on a predicate; unlike MapWhen, its branch is rejoined to the main pipeline if it has no terminal middleware.
See also
Claims
- id: C1 claim: The pipeline is a sequence of request delegates called one after the other. source: S2 evidence: "consists of a sequence of request delegates, called one after the other"
- id: C2 claim: A short-circuiting middleware is called terminal middleware. source: S2 evidence: "it's called a terminal middleware because it prevents further middleware"
- id: C3 claim: Do not call next.Invoke after the response has been sent. source: S2 evidence: "Don't call
next.Invokeafter the response has been sent to the client."
- id: C4 claim: Request delegates are configured with Run, Map and Use. source: S1 evidence: "Request delegates are used to build the request pipeline."
Sources
- [S1] ASP.NET Core Middleware. Microsoft Learn (ASP.NET Core docs). https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/ . License: CC-BY-4.0. Accessed 2026-11-18.
- [S2] ASP.NET Core Middleware (source page). dotnet/AspNetCore.Docs (GitHub). https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/middleware/index.md . License: CC-BY-4.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (ASP.NET Core docs), ASP.NET Core Middleware; dotnet/AspNetCore.Docs (GitHub), ASP.NET Core Middleware (source page). CC-BY-4.0 (https://raw.githubusercontent.com/dotnet/AspNetCore.Docs/main/LICENSE). Copyright Microsoft Corporation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (ASP.NET Core docs), ASP.NET Core Middleware; dotnet/AspNetCore.Docs (GitHub), ASP.NET Core Middleware (source page). CC-BY-4.0 (https://raw.githubusercontent.com/dotnet/AspNetCore.Docs/main/LICENSE). Copyright Microsoft Corporation.
Cite this
iinventedlight. 2026. "ASP.NET Core middleware runs as an ordered pipeline built with Use, Map and Run." iinventedlight Library, iil:note:kd7v4fyv, version of 2026-11-18. https://iinventedlight.com/library/notes/aspnetcore-middleware/. License: CC-BY-4.0.
@misc{iil_note_kd7v4fyv,
author = {{iinventedlight}},
title = {{ASP.NET Core middleware runs as an ordered pipeline built with Use, Map and Run}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/aspnetcore-middleware/},
note = {Permanent ID iil:note:kd7v4fyv. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/aspnetcore-middleware.md.