Open In App

Middleware in ASP.NET Core

Last Updated : 08 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will understand what middleware is in asp.net core. A Middleware is a very broad term in asp.net core middleware is a piece of software that can handle an HTTP request or response. For example, we may have a middleware of a component that authenticates a user, another piece of middleware to handle errors yet another middleware to serve static files such as JavaScript files CSS files images, etc.

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.

Middleware Ordering

 

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.

Request pipeline short-circuiting is advisable because it prevents unnecessary work, for instance, if the request is made to get static files (image, CSS, JavaScript files, etc). With these static files, middleware can handle and serve that request and then short-circuit the rest of the pipeline this means in our case the static files. A Middleware will not call the next piece of middleware which is the MVC middleware if the request is for a static file like an image.

For example, a middleware component may also simply ignore the incoming request and then pass that request on to the next piece of middleware for further processing. let’s say this time we are issuing a request to a local host for /employees basically we want to retrieve the list of all employees so when this request arrives from the webserver at our first middleware in the pipeline which is logging this middleware simply logs the time the request is received, and then pass that request to the next piece of middleware in our case to static files middleware since this request is not for a static file like an image CSS file JavaScript file, etc. 

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads