In this article we will discuss Middleware in ASP .NET Core. Before starting this article please go through our previous article ASP.NET Core appsettings.json file. In this article we cover below points.
- What is Middleware Components in ASP.NET Core ?
- Where we use the Middleware Components in the ASP.NET Core application ?
- How to Configure Middleware Components in ASP.NET Core application ?
- Examples of using Middleware Components ?
- What is the Execution Order of Middleware Components in ASP.NET Core ?
➤ What is Middleware Components in ASP.NET Core
In ASP.NET Core, Middleware is a piece of software that can handle an HTTP request or response. Each middleware component in ASP.NET Core Application performs the following tasks.
- Chooses whether to pass the HTTP Request to the next component in the pipeline.
- Can perform work before and after the next component in the pipeline.
In ASP.NET Core there are so many built-in Middleware components are already available that you can directly use. If you want then you can also create your own Middleware components. The most important point that you need to remember is, in ASP.NET Core a given Middleware component should only have use in a specific purpose.
➤ Where we use the Middleware Components in the ASP.NET Core application ?
- We may have a Middleware component for authenticating the user.
- Another Middleware component may be used to log the request and response.
- Similarly, we may have a Middleware component that is used to handle the errors.
- We may have a Middleware component that is used to handle the static files such as images, Javascript or CSS files, etc.
- Another Middleware component may be used to Authorize the users while accessing a specific resource.
If you have worked with previous versions of ASP.NET then you may know, we use HTTP Handlers and HTTP Modules to set up the request processing pipeline, this pipeline which will determine how the HTTP request and response is going to be processed.
Similarly in ASP .NET Core, the Middleware components are the components that we use to set up the request processing pipeline in the ASP.NET Core application.
➤ How to Configure Middleware Components in ASP.NET Core application ?
In ASP.NET Core application, you need to configure the Middleware components within the Configure() method of the Startup class which is present within the Startup.cs file. This is the class that is going to run when the application starts. When we create an ASP.NET Core application with Empty Template, then by default the Startup class is created with the Configure() method as shown in the below image.
Fig-1 |
Whenever you want to configure any middleware components, then you need to configure it within the Configure() method of the Startup class by calling the Use* methods on the IApplicationBuilder object. As you can see from the above image, the configuration() method sets up the request processing pipeline with just two middleware components are as follows
- UseDeveloperExceptionPage() Middleware component
- Run() Middleware component
➤ Understanding the Middleware Components in ASP.NET Core
The above diagram explains what the middleware components are and how they used in the request processing pipeline of an ASP.NET Core application.
In ASP.NET Core application, the Middleware component can have access to both the incoming HTTP Request and outgoing HTTP Response. So a Middleware component in ASP.NET Core can
➤ Examples of Middleware Components ?
Fig-2 |
In ASP.NET Core application, the Middleware component can have access to both the incoming HTTP Request and outgoing HTTP Response. So a Middleware component in ASP.NET Core can
- Handle the incoming HTTP request by generating an HTTP response.
- Process the incoming HTTP request, modify it, and then pass it to the next middleware component
- Process the outgoing HTTP response, modify it, and then pass it on to either the next middleware component or to the ASP.NET Core web server.
As shown in Fig-2, we have a logging middleware component. This component simply logs the request time and then passes the request to the next middleware component i.e. Static Files Middleware component in the request pipeline for further processing.
A middleware component in ASP.NET Core may also handle the HTTP Request by generating an HTTP Response. The ASP.NET Core Middleware component may also decide not to call the next middleware component in the request pipeline. This concept is called short-circuiting the request pipeline.
For example, we have a static file middleware component. And if the incoming HTTP request comes for some static files such as images, CSS files, etc. then this Static Files Middleware component can handle the request and then short-circuit the request pipeline by not calling to the next component in pipeline i.e. the MVC Middleware component.
As we already discussed the ASP.NET Core middleware components can have access to both the HTTP request and response in the pipeline. So a middleware component can also process the outgoing response. For example, the logging middleware component in our case may log the time when the response is sent back to the client.
➤ Execution Order Middleware Components in ASP.NET Core Application ?
The ASP.NET Core middleware components are executed in the same order as they are added to the pipeline. So we need to take care when adding the middleware components to the request processing pipeline.
As per your applications business requirements, you may add any number of Middleware components. For example, if you are developing a static web application with some static HTML pages and images, then you may require only "StaticFiles" middleware components in the request processing pipeline.
But, if you are developing a secure dynamic data-driven web application then you may require several middleware components such as Logging Middleware, Authentication middleware, Authorization middleware, MVC middleware, etc.
Now we have a basic understanding of what middleware components are and how they fit in a request processing pipeline, in our next article, we will understand, how to configure a request processing pipeline for our ASP.NET Core application using middleware components.
Summary
You May Also Like...
- ASP.NET Core appsettings.json file
- ASP .NET Core launchSettings.json file
- ASP.NET Core Out Of Process Hosting
- Kestrel Web Server in ASP.NET Core
- ASP.Net Core Hosting model
- Main method in ASP.NET Core
- ASP .NET Core MVC Project Structure & Process Flow
- ASP .Net Core Hello World Program
- How to Create Windows Service in .NET Core
Post a Comment