liveBook · Manning

Branch

As explained in ASP.NET Core in Action, 3e

A middleware pipeline branch is an alternative request-processing path selected within a middleware pipeline.

A middleware pipeline branch is an alternative request-processing path. In ASP.NET Core, a request matching a path prefix enters the branch; other requests continue along the main pipeline. section 31.1.2

Use Map when different URL areas need different middleware behavior. A request enters one branch or the trunk, never both. MapWhen provides a similar choice based on any HttpContext value, such as a header or query string. section 31.1.2

A /ping branch

app.Map("/ping", branch => { branch.UseExceptionHandler(); branch.Run(async context => { context.Response.ContentType = "text/plain"; await context.Response.WriteAsync("pong"); }); }); app.UseStaticFiles(); app.UseRouting(); app.MapRazorPages(); A request to /ping receives pong from the branch. Other requests continue to static files, routing, and Razor Pages. Run should terminate the branch because middleware placed after it will not execute. section 31.1.2 section 31.4

Figure 31.1 A sequential middleware pipeline compared with a branching pipeline created with the Map extension. In branching middleware, requests pass through only one of the branches at most. Middleware on the other branch never see the request and aren’t executed.
CH31_F01_Lock3

Compare a sequential pipeline with a branching pipeline: matched requests enter only the selected branch, while middleware on the other branch never executes. section 31.1.2

Map performs a simple string-prefix match rather than full routing and does not support route parameters. When it matches, it moves the matched path segment from HttpContext.Path to PathBase so routing inside the branch works correctly. section 31.1.2 section 31.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains Map selection, terminal Run behavior, path handling, and limitations, but defers detailed endpoint-routing implementation.
Examples
Demonstrates a /ping branch that returns plain-text pong while Razor Pages and static files handle other requests.
What it assumes
Assumes familiarity with sequential middleware and introduces branch selection through a small request-response example.
What you can do after
Operate/configure an ASP.NET Core pipeline with Map, a branch-specific middleware sequence, and a terminal response.
How it connects
Connects middleware pipelines with MapWhen, HttpContext, endpoint routing, health checks, and terminal middleware.

Also explained in

Compare how 6 books explain branch →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.