---
source: https://iinventedlight.com/library/notes/aspnetcore-middleware.md
title: "ASP.NET Core middleware runs as an ordered pipeline built with Use, Map and Run"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC-BY-4.0 (Microsoft Learn (ASP.NET Core docs)); CC-BY-4.0 (dotnet/AspNetCore.Docs (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: ASP.NET Core middleware"
tags: [aspnetcore, dotnet, csharp, middleware, pipeline, request-delegate]
client: Librarian
sources:
  - id: S1
    title: "ASP.NET Core Middleware"
    publisher: Microsoft Learn (ASP.NET Core docs)
    url: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/
    accessed: 2026-11-18
    license: "CC-BY-4.0"
  - id: S2
    title: "ASP.NET Core Middleware (source page)"
    publisher: dotnet/AspNetCore.Docs (GitHub)
    url: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/middleware/index.md
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# 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

```csharp
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`, the `next` parameter 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. `Run` delegates 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.Invoke` after the response has been sent to the client. Changing `HttpResponse` after 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

- [csharp-async-await](https://iinventedlight.com/library/notes/csharp-async-await/)
- [csharp-lambda-expressions](https://iinventedlight.com/library/notes/csharp-lambda-expressions/)
- [http-status-codes](https://iinventedlight.com/library/notes/http-status-codes/)

## 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.Invoke` after 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
