liveBook · Manning

Middleware ordering

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

Middleware ordering is the arrangement of request-processing components that determines their execution sequence and response behavior.

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.
CH04_F11_Lock3

Use the pipeline diagram to trace how the order added to WebApplication becomes the execution order for requests. section 4.2.3

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

How deep it goes
Explains sequencing, automatic middleware, and short-circuiting through minimal API examples, without detailing framework internals.
Examples
Uses a minimal API, a Welcome-page middleware, static files, routing, endpoint mapping, and pipeline figures.
What you can do after
Evaluate request behavior by tracing middleware sequence and identifying components that short-circuit the pipeline.
How it connects
Connects middleware ordering with routing, endpoint handling, static files, exception handling, authentication, and authorization.

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.