Middleware in ASP.NET Core
It is these middleware components that we use to set up a request processing pipeline it is this pipeline that determines how a request is processed the request pipeline is configured as part of the application startup by this configure method.
Now let’s understand how middleware works in general in asp.net core.
A middleware of a component in a spirited core has access to both the incoming requests and the outgoing response so in a merger, a component may process an incoming request and then pass that request to the next piece of middleware in the pipeline for further processing.
For example, let’s say logging middleware is the first middleware in our application request processing pipeline, so when a request arrives from the webserver this middleware logs the time that the request is received and then passes that incoming request to the next piece of middleware in our case to the static files middleware for further processing a middleware component may handle the incoming requests and decide not to call the next piece of middleware in the pipeline this is called short-circuiting.
Static files middleware may not be interested in processing this request so it will simply pass that request to the next piece of middleware which is MVC middleware this is an MVC request so the MVC middleware knows how to handle that request so the respective MVC controller produces the HTTP response at this point the pipeline reverses itself the MVC middleware passes that response to the static files, where the static files middleware is not interested in processing that response further, So it may simply pass that responsibility to the next piece of middleware which is the logging middleware. This middleware may simply lock the time the response is received remember this logging middleware also has the time it received the request. So using these two times it may also compute the overall time taken to service that request and then pass that response to the web server in turn may send that response to the client who made the request so it is these middleware components in the pipeline that determine how a request is processed in a split or net core these middleware components are executed in the order they are added to the pipeline care should be taken to add the middleware in the right order. otherwise, the application may not function as expected.
To use the middleware in the application, Using the request delegates we have to build the request pipeline which handles the HTTP request. We can configure the request delegates through the ‘Run’, ‘Map’, and ‘Use’ methods on IApplicationBuilder.
These delegates will decide whether the request should be passed to the next component in the application pipeline or not. It can also perform the actions such as saving data, before and after the invocation of the next component in the application pipeline. Then the response comes back in a similar way through the pipeline.