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
