liveBook · Manning

Mini pipeline

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

A mini pipeline is an ordered sequence of middleware components that processes requests for an individual endpoint.

A mini pipeline is an ordered sequence of middleware components that processes requests for one endpoint. It lets response-generating middleware become an endpoint without changing the middleware itself. section 31.1.5

Build a mini pipeline by following these steps:

  • Create an IApplicationBuilder through CreateApplicationBuilder() on an IEndpointRouteBuilder. section 31.1.5
  • Add the endpoint’s middleware, then call Build() to produce the completed pipeline. section 31.1.5
  • Associate the built pipeline with a route by calling Map() on the endpoint route builder. section 31.1.5

Mapping a ping endpoint

A standalone PingPongMiddleware writes a plain-text "pong" response. Build it into a mini pipeline and map that pipeline to /ping. Endpoint routing selects the route, then executes the pipeline. section 31.1.5

Listing 31.10 Mapping the ping-pong endpoint in UseEndpoints
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
 
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
var endpoint = ((IEndpointRouteBuilder)app)     #1
    .CreateApplicationBuilder()                 #2
    .UseMiddleware<PingPongMiddleware>()        #3
    .Build();                                   #3
 
app.Map("/ping", endpoint);                     #4
app.MapRazorPages();
app.MapHealthChecks("/healthz");
app.Run();
#1 Casts the WebApplication to IEndpointRouteBuilder so you can call CreateApplicationBuilider
#2 Creates a miniature, standalone IApplicationBuilder to build your endpoint
#3 Adds the middleware and builds the final endpoint. This is executed when the endpoint is executed.
#4 Maps the new endpoint with the route template “/ping”

Shows the complete setup: casting WebApplication to IEndpointRouteBuilder, creating the standalone builder, adding middleware, building the pipeline, and mapping /ping. section 31.1.5

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

How deep it goes
Explains construction and routing through code, while signaling that CreateApplicationBuilder() belongs to advanced configuration.
Examples
Uses PingPongMiddleware mapped to /ping alongside Razor Pages and health checks.
What you can do after
Build an endpoint pipeline from middleware and map it to a route with ASP.NET Core routing APIs.
How it connects
Connects middleware pipelines with endpoint routing, minimal APIs, filters, and route groups.

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.