What is Middleware?
Middleware is software that is assembled into application pipelines to handle requests and responses. Each component:
- Choose whether to pass the request to the next component in the pipeline.
- Work can be performed before and after the next component in the pipeline is invoked.
Request delegates are used to build a request pipeline that handles each HTTP request.
Request delegates are configured using the Run, Map, and Use extension methods. A separate request delegate can be specified in an inline anonymous method (called inline middleware), or it can be defined in a reusable class. These reusable classes and inline anonymization methods are middleware or middleware components. Each middleware component in the request flow is responsible for calling the next component in the pipeline and, if appropriate, the link short circuit.
Migrating HTTP modules to middleware explains the difference between ASP.NET Core and request pipelines in previous versions (ASP.NET) and provides more middleware examples.
Use IApplicationBuilder to create a middleware pipeline
The ASP.NET Core request process consists of a series of request delegates, as shown in the following figure (the execution process follows the black arrow):
Each delegate can perform actions before and after the next delegate. The delegate can also decide not to pass the request to the next delegate, which is called a short circuit in the request pipeline. A short circuit is usually desirable because it avoids unnecessary work. For example, static file middleware can return a request for a static file and short circuit the rest of the pipeline. Exception handling delegates need to be invoked early in the pipeline, so they can catch exceptions in later pipelines.
The simplest thing is probably to set up a delegate ASP.NET the Core application to handle all requests. This scenario does not include the actual request pipeline. Instead, an anonymous method is called for each HTTP request.
The first app. Run delegate terminates the pipeline.
There is code like this:
Accessed through the browser, it is found that it is indeed in the first app. Run terminates the pipeline.
You can delegate multiple requests with the app. Use is connected together. The next parameter represents the next delegate in the pipeline. (Keep in mind that you can end the pipeline by not calling the next parameter.) You can usually perform actions before and after the next delegation, as shown in the following example:
Using a browser to access results such as:
It can be seen that the execution order of the request delegate follows the flow chart above.
Note:
After the response is sent to the client, do not call next. Invoke。 After the response starts, changes to the HttpResponse will throw an exception. For example, setting response headers, status codes, etc. will throw an exception. Write the response body after calling next.
may result in a breach of the agreement. For example, writing more than the content length described in content-length.
Responsive content formatting may be broken. For example, write an HTML footer into a CSS file.
HttpResponse.HasStarted is a useful hint to indicate whether a response header has been sent and/or the body has been written.
Built-in middleware
ASP.NET Core comes with the following middleware components:
| Middleware | description | | Authentication | Authentication support is available | | CORS | Configure cross-domain resource sharing | | Response Caching | Cache response support is available | | Response Compression | Provide support for response compression | | Routing | Define and constrain request routing | | Session | Provides user session management | | Static Files | Provides support for static file and directory browsing | | URL Rewriting Middleware | Used to rewrite URLs and request support for redirects |
|