Middleware in ASP.NET Core
Last Updated :
08 Apr, 2022
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.
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.
Similar Reads
ASP.NET MVC Life Cycle
The goal of this article is to provide a good understanding of the MVC pipeline. The life cycle is basically is set of certain stages which occur at a certain time. MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The application life cycle, in which t
8 min read
ASP.NET MVC Caching
Caching is the data store just like a database but slightly it is different cache stores, its data in memory so it does not interact with the file system. It also stores data in very simple structures like key-value pairs. So they don't have to execute complex queries to gather data and maintain ind
7 min read
Managed and Unmanaged Code in .NET
In .NET, the terms "Managed Code" and "Unmanaged Code" refer to how the code is executed and managed by the system. Managed code runs under the control of the CLR(Common Language Runtime), while unmanaged code runs directly on the operating system. Each type of code has its own advantages and is use
3 min read
Creation of Custom Middleware using IMiddleware Interface in ASP.NET Core
The IMiddleware Interface in ASP.NET Core is a contract for creating a custom middleware that can be added to the application's existing pipeline. This interface was introduced in ASP.NET Core to provide an alternative way to create middleware components, apart from the traditional RequestDelegate-b
2 min read
Differences Between .NET Core and .NET Framework
.NET Core is a free open source, a general-purpose development platform for developing modern cloud-based software applications on Windows, Linux, and macOS operating systems. It operates across several platforms and has been revamped to make .NET fast, scalable, and modern. .NET Core is one of Micr
4 min read
Difference Between C# and ASP.NET
Pre-requisites: C#, ASP.NET C# (also known as C sharp) is an object-oriented programming language that is used to produce an array of applications for gaming, mobile, web, and Windows platforms also It is a modern and type-safe language and provides simple syntax which makes it easier to learn and i
2 min read
Difference Between .NET and ASP.NET Framework
.NET Framework is used to develop Form-based applications, Web-based applications, and Web services. It is used to develop both desktops as well as server-based applications. There is a variety of programming languages available on the .Net platform, VB.Net, and C# being the most common ones. It is
2 min read
How to Become a C# Developer
In the field of computer programming, certain skills are in high demand, with C# and the .NET framework standing out among the most sought-after. C# Developers play a crucial role in designing and developing modern applications, including desktop, mobile, cloud, and even game applications. This guid
8 min read
.NET Framework Class Library (FCL)
The Framework Class Library or FCL provides the system functionality in the .NET Framework as it has various classes, data types, interfaces, etc. to perform multiple functions and build different types of applications such as desktop applications, web applications, mobile applications, etc. The Fra
3 min read
Difference Between Node.js and Asp.net
ASP.NET: It is an open-source web application framework initially discharged by Microsoft in January 2002 with the primary cycle of the .NET system. It is built on the Common Dialect Runtime (CLR), permitting the utilization of any .NET dialect counting C# (object-oriented), F# (utilitarian to begin
4 min read