This article is a mirror article of machine translation, please click here to jump to the original article.

View: 45031|Reply: 1

[ASP.NET] ASP.NET Detailed explanation of the Middleware middleware of Core (9).

[Copy link]
Posted on 2021-5-12 11:21:52 | | | |
Middleware is a type of software that is assembled into an application pipeline 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.
Request delegation is used to build a request pipeline. Request delegates to handle each HTTP request.

Documentation:The hyperlink login is visible.

Review:
ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
ASP.NET Core MiddlewareMiddleware is registered in a singleton model, that is, the application will only be instantiated once and the constructor will only be initialized once from start to finish.

First, let's create a new TestMiddleware middleware with the following code:


Register in the Configure method of the startup file, as follows:

Middleware intercepts direct response requests

Try to intercept the request for the /home/index2 path and directly respond to a piece of JSON message, the code is as follows:



Middleware middleware passes parameter values

Sometimes, we need to pass some parameter configurations to middleware, such as: intercepted address, response information, etc.

UseMiddleware() method to pass the params object[] parameter to the middleware's constructor, which means that we can pass any number of parameters to the middleware through params object[], as long as the type and the middleware's constructor parameters match.

It can be passed by code like this:




Middleware middleware is passed between values

Create a new Test2Middleware middleware, we register multiple middleware at the same time, and the subsequent middleware needs to do some logical processing according to the previous middleware, that is, pass parameters between middleware, the code is as follows:




Middleware OnStarting responds to the delegation

Because middleware can register multiple and may go through multiple intermediate processes, the last middleware wants to do something before responding, it can register an OnStarting delegate. The test will perform a header change in the context of HttpResponse.OnStarting(Func<Task>) to ensure that the callback is executed immediately before flushing the response to the client.


The code is as follows:



(End)




Previous:EF Entities of append type "XXXX" fail because other...
Next:POI uses three methods: HSSF, XSSF, SXSSF
Posted on 2021-9-22 19:59:27 |
Learn to learn...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com