Middleware classification


ASP.NET Core There are three ways to configure Middleware , Corresponding Helper The methods are :Run(), Use(), Map().

* Run(), Use Run When calling Middleware , A response will be returned directly , So the subsequent middleware will not be executed .
* Use(), It does some work or processing on the request , For example, add some requested context data , Sometimes you don't even do anything , Direct the request to the next Middleware .
* Map(), It will reroute the request to other middleware paths .
In practice? ,Use() this helper Most methods are used .




  This is a use Run Method call Middleware ,Run Method will terminate the entire middleware pipeline , It should return some kind of response .




 Use Looks like Run Almost , But one more next parameter .next Can be used to call the next Middleware in the request pipeline . The current middleware can also return the response itself , That's ignored next call .

stay next Before calling , We can write some logic for requesting in , While in next After calling , It's like returning a response , You can also write some logic at this time .

In this case , We also use Run Method registers another Middleware . Because middleware will be called in the order they are registered , So in the first Use Method next.Invoke() When , We'll do the following Run Middleware called .




 Map Method can route the request to other middleware .

Ad locum , If the requested path /jump Ending , So it corresponds to handler Method , that is HereIAm Method will be called , And return a response .

If the requested path is not /jump Ending , that HereIAm Methods and middleware inside will not be called .


middleware Class

The above example , I use them all inline Writing Middleware .

But actually , Middleware is usually a class of its own . Middleware classes need to be similar to this :


  The custom middleware class needs to be composed of these parts :

* Accept one RequestDelegate Parameters of type next Constructor for .
By appointment , There's also a definition called Invoke Method . This method will contain the main business logic , And it will be executed by the requested pipeline .Invoke Method can ignore the _next call , And return a response ; You can also call _next.Invoke() Send the request to the next station in the pipeline .

Middleware flow chart






Endpoint Routing Routing system

ASP.NET Core 3.x Used a set called Endpoint Routing Routing system of . This routing system ASP.NET Core
2.2 It was already there by the time .

This set of Endpoint Routing Routing system provides more powerful functions and flexibility , In order to better handle requests .


early stage ASP.NET Core Routing system of

Let's review earlier versions of ASP.NET Core Routing system of :



Core In frame ,HTTP Request to enter middleware pipeline , At the end of the pipe , There is one Router middleware , Routing Middleware . This routing middleware will HTTP Request and route data to MVC A component of , It's called MVC
Router Handler.

this MVC Route Handler It will use these routing data to decide which Controller Of Action Method should handle the request .

Then? Router Middleware will execute the selected Action Method , And generate a response , And this response will return along the middleware pipeline .


What's the problem ?

Why was the early routing system abandoned ? What's wrong with it ?

The first question is , In being MVC Before processing , Other middleware don't know the last one Action Method will be selected to process the request . That's right. Authorization( To grant authorization ),Cors Such middleware will cause great trouble , Because they can't know in advance where the request will be sent .

The second question is , This process will MVC Closely coupled with routing responsibilities , But actually MVC Our job is just to generate response .


Endpoint Routing Routing system to rescue

Endpoint routing Routing system , It takes MVC The routing function of , And put it in the middleware pipeline , So that the early ASP.NET
Core The two problems of routing system .

While in Endpoint Routing In the routing system , In fact, there are two Middleware , Their names are a little confusing , But you just need to remember their responsibilities :

* Endpoint Routing middleware . It determines which one is registered in the program Endpoint Should be used to process requests .
* Endpoint middleware . It is used to execute the selected Endpoint, So that it can generate a response .

therefore ,Endpoint Routing The flow chart of is roughly as follows :


  Ad locum ,Endpoint Routing Middleware will analyze incoming requests , And it's the same as the Endpoints Compare . It will use these Endpoints
The metadata above determines which is the best person to process the request . Then? , This one was chosen Endpoint
Will be assigned to the requested object , Other subsequent middleware can be selected according to this Endpoint, To make your own decisions . After all the middleware has been executed , This one was chosen Endpoint Will eventually be
Endpoint Executed by middleware , And the Action Method will be executed .


Endpoint What's this ?

Endpoint There are some classes like this , These classes contain a requested delegate (Request
Delegate) And some other metadata , Use these things ,Endpoint Class can generate a response .

While in MVC In the context of , This request delegate is a wrapper class , It packages a way , This method can instantiate a Controller And execute the selected Action Method .

Endpoint Also contains metadata , This metadata is used to determine whether their request delegation should be used for the current request , Or something else .

It may be a little confusing , Let's take a look at the source code later .



We met before ,ASP.NET
Core Inside Startup.cs There are two ways , Namely ConfigureServices() and Configure(), Their responsibilities are to register some services of the application and build the middleware request pipeline .

and Startup.cs It is also the application route and Endpoint Place to register as part of other steps .

Look at the picture :



  stay ASP.NET
Core When the application starts , One is called. ControllerActionEndpointDataSource Class of is created as an application level service .

There is a class called CreateEndpoints() Method , It will get all Controller Of Action Method .

Then for each Action Method , It will create a Endpoint Example . These ones here Endpoint The example is packaging Controller and Action Request delegation for execution of method (Request


ControllerActionEndpointDataSource It stores the route template registered in the application .

For each Endpoint, It is either associated with an agreed routing template , Or with some Controller
Action Upper Attribute Associated routing information . These routes will be used later to Endpoint Match incoming request .


from Endpoint Angle view request for - Response flow chart


App Let's not talk about the startup part .

First HTTP When you ask to come in ,Endpoint
Routing Middleware will map the request to a Endpoint upper . It will use it App Created at startup EndpointDataSource, To traverse through all available Endpoint, And check the route and metadata associated with it , To find the best match Endpoint.

Once a certain Endpoint Instance selected , It will be attached to the requested object , So it can be used by subsequent middleware .

At the end of the pipe , When
Endpoint When middleware is running , It will execute Endpoint Associated request delegation . This request delegation will trigger and instantiate the selected Controller and Action Method , And generate a response . Finally, the response is returned from the middleware pipeline .


©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network