A cross-cutting concern is application behavior needed across many request paths rather than one resource. Logging, error handling, security, user association, and request language are common examples. In ASP.NET Core, middleware is the preferred place to configure and handle these behaviors because every request passes through the middleware pipeline. section 4.1 section 4.0
Middleware handles a cross-cutting concern by processing the request before passing it onward, then participating again as the response returns.
- Authentication can associate a request with a user; authorization can then check permissions for that request. section 4.1
- A component can short-circuit the pipeline and return a response directly, becoming terminal middleware. section 4.1
- Middleware applies broadly, while filters suit behavior that needs MVC constructs or only selected controllers, pages, or actions. section 21.3
A request pipeline
A request can pass through logging middleware, security-related middleware, authentication, authorization, and endpoint middleware. Authorization either forwards an allowed request so the endpoint can generate a response or stops a forbidden request before the endpoint runs. section 4.1
Figure 4.3 You can also think of middleware as being a series of nested components; a request is sent deeper into the middleware, and the response resurfaces from it. Each middleware component can execute logic before passing the response on to the next middleware component and can execute logic after the response has been created, on the way back out of the stack.
The nested-pipeline view shows how middleware runs on the way in and can run again on the way out, around response creation. section 4.1

