liveBook · Manning

Middleware

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

Middleware is a request-processing component that can inspect, modify, or handle HTTP requests and responses within an ASP.NET Core pipeline.

Middleware is a small C# component that handles an HTTP request or response. In an ASP.NET Core application, components form a pipeline: a request moves through them in order, and the response travels back through them in reverse order. This lets each component take care of one job, such as logging, serving a file, or handling an error. section 4.1 section 4.2

Figure 4.1 Example of a middleware pipeline. Each middleware component handles the request and passes it on to the next middleware component in the pipeline. After a middleware component generates a response, it passes the response back through the pipeline. When it reaches the ASP.NET Core web server, the response is sent to the user’s browser.
CH04_F01_Lock3

Follow the request inward and the response outward. A component can act at either point. section 4.1

The web server creates an HttpContext for each request and passes it into the pipeline. Middleware can change that context for later components, pass the request along unchanged, or generate a response itself. If it responds without calling the next component, it short-circuits the pipeline: later middleware and endpoints do not run for that request. If nothing handles a request, the pipeline returns a 404 status code. section 3.1 section 4.4

Start with a holding page

Create a WebApplication, call app.UseWelcomePage(), then app.Run(). The welcome-page middleware responds with the same sample HTML page at every path. It is a quick way to check that the application is receiving requests, not a customizable production page. section 4.2.1

Add middleware to WebApplication with Use* methods in Program.cs. Registration order matters: a component can use data established by earlier middleware, but not data established by later middleware. Place error handling before components whose errors it should catch. A component that handles a request early can prevent everything after it from running. section 3.7.2 section 4.2.3 section 13.1.2

Add request logging

Register HTTP logging services on WebApplicationBuilder, build the app, then call app.UseHttpLogging() when app.Environment.IsDevelopment(). MapGet("/person", ...) defines an endpoint that returns JSON; it does not add middleware. In development, a request to /person produces the JSON response and logs request details to the console. section 3.7 section 4.2.3

RoutingMiddleware selects an endpoint; EndpointMiddleware executes it. Other middleware can serve static files before routing or check authorization after routing has identified the endpoint. For behavior needed across requests, middleware is generally a good fit. When behavior needs an endpoint’s return value or MVC-specific information, consider an endpoint or MVC filter instead. section 14.1 section 5.4.2 section 21.3

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

How deep it goes
Explains two-way flow, short-circuiting, ordering, and composition with existing components; defers building custom middleware.
Examples
Uses a welcome page, development-only HTTP logging, and a minimal API returning JSON to show pipeline behavior.
What it assumes
Assumes basic C# and HTTP vocabulary while introducing the pipeline through a small WebApplication.
What you can do after
Operate/configure it through WebApplication Use* calls, ordering components and choosing middleware rather than filters when endpoint-specific details are unnecessary.
How it connects
Connects middleware to HttpContext, routing, endpoints, static files, exception handling, and endpoint filters.

Also explained in

Compare how 10 books explain middleware →

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.