What is the middleware in ASP.NET Core?
In ASP.NET Core, Middleware is a software pipeline that can handle HTTP requests or responses. Middleware components are positioned in ASP.NET Core for a very specific purpose. For example, we might need one middleware component to validate the user, another middleware to handle errors, another middleware to provide static files such as JavaScript files, CSS files, images, and so on.
We use these middleware components to set up the request processing pipeline in ASP.NET Core. These pipes determine how requests are handled. The request pipe is configured by the Configure() method in the startup. cs file and is an important part of application Startup. Here is the code in the Configure() method.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); App. The Run (async (context) = > {/ / / / acquisition Process name / / var processName = System. Diagnostics. The Process. The GetCurrentProcess (), processName; var configVal = Configuration["MyKey"]; await context.Response.WriteAsync(configVal); }); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }Copy the code
As you can see, in the code in the Configure() method generated by the empty project template, there is a very simple request processing pipeline with multiple middleware.
Now, with this very simple request processing pipeline, all of our applications can write messages and display them in the browser.
Now let’s see what middleware is and how it works in ASP.NET Core.
The following diagram helps us understand the middleware components and how they fit into the request processing pipeline
In ASP.NET Core, middleware components can be accessed simultaneously –Incoming requests and outgoing responses. Thus, the middleware component can process an incoming request and pass it to the next middleware in the pipeline for further processing. For example, if you have a logging middleware, it might just log the time of the request, and when it’s finished processing it will pass the request to the next middleware for further processing.
A middleware component can process requests and decide not to call the next middleware in the pipeline, thus short-circuiting the pipeline. The official Microsoft name for middleware is “Terminal Middleware.” Short circuit is usually allowed because it can avoid some unnecessary work. For example, if the request is for a static file such as an image or CSS file, the StaticFiles middleware can process and service the request and short-circuit the rest of the pipe. This means that in our example, if the request is for a static file, the Staticile middleware will not call the MVC middleware, avoiding unnecessary operations.
A middleware component can respond to an HTTP request with an incoming HTTP request. For example, mvcmiddleware in the pipeline processes requests for URL/ Employee and returns employee list information.
Middleware components can also handle outgoing responses. For example, a logging middleware component can record when a response was sent. In addition, it can calculate the total time taken to process the request by calculating the difference between the time the request was received and the time the response was sent.
The middleware components are executed in the order they were added to the pipeline. So be careful to add the middleware in the right order, otherwise the application may not run as expected, and even if it compiles successfully, the program will still fail.
Middleware components should be provided as NuGet packages. NuGet handles updates, making the middleware small enough to provide the ability for each middleware to update independently.
Depending on your program requirements, you can add as many middleware components as possible to the request processing pipeline. For example, if you are developing a simple Web application with some static HTML pages and images, your request processing pipeline may contain only the “StaticFiles” middleware. And that’s the benefit of modular design, which lets everyone play with blocks. On the other hand, if you are developing a Web application with a secure data-driven design, you may need several middleware components such as StaticFiles middleware, authentication middleware, authorization middleware, MVC middleware, and so on. Data-driven design can be simply understood as complex projects.
What I want to point out is that the system is yours, you are the architect of the system, and you have full control over the requests, handling the configuration in the pipeline. This also means that, from a memory and performance perspective, you only have to pay traffic fees and server expenses for the middleware components in the request processing pipeline.