Middleware ordering arranges request-processing components in a pipeline. The order determines which component receives a request first, which response is produced, and whether later components run. section 4.2.3
A minimal API pipeline can combine components with different responsibilities:
- Exception handling can capture errors. section 4.2.3
- Static-file middleware can serve image files. section 4.2.3
- Routing middleware chooses the endpoint, which then generates the response. section 4.2.3
- A component can short-circuit the pipeline, preventing later middleware from running. section 4.2.3
Changing the response by changing the order
Place app.UseWelcomePage("/") before exception handling, static files, routing, and app.MapGet("/", ...). A request to / receives the Welcome-page HTML response, and the minimal API endpoint does not run. Moving that middleware changes which component handles the request first. section 4.2.3
Figure 4.11 The middleware pipeline for the example application in listing 4.3. The order in which you add the middleware to WebApplication defines the order of the middleware in the pipeline.
Use the pipeline diagram to trace how the order added to WebApplication becomes the execution order for requests. section 4.2.3
