Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The ASP.NET Core Middleware Components are the software components (C#
Classes) which are assembled into the application pipeline to handle the HTTP
Requests and Responses. Each middleware component in ASP.NET Core
1. Chooses whether to pass the HTTP Request to the next component in the pipeline.
2. 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 a specific purpose.
The Middleware components are the components which we use to set up the request
processing pipeline in the ASP.NET Core application. 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. It is this pipeline which will determine how the
HTTP request and response is going to be processed.
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 which 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.
So, 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
In our next article, we will discuss these two Middleware components in details. For
now, let us understand what are Middleware components and how exactly these
Middleware components are going to work in the ASP.NET Core application.
The below 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
1. Handle the incoming HTTP request by generating an HTTP response.
2. Process the incoming HTTP request, modify it, and then pass it to the next
middleware component
3. Process the outgoing HTTP response, modify it, and then pass it on to either next
middleware component or the ASP.NET Core web server.
Examples:
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 files 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.
As per your application’s 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.
In our next article, we are going to discuss how to configure middleware
components in an ASP.NET Core application to handle the request processing
pipeline.
SUMMARY
In this article, I try to explain the ASP.NET Core Middleware Components in detail. I
hope this article will help you with your need. I would like to have your feedback. Please
post your feedback, question, or comments about this article.
In ASP.NET Core, the Request delegates are used to build the request pipeline. That
means the request delegates are used to handle each incoming HTTP request. You can
configure the Request delegates using the Run, Map, and Use extension methods. You
can specify an individual request delegate using an in-line anonymous method which is
also called as in-line middleware, or you can specify the request delegates using a
reusable class. These reusable classes and in-line anonymous methods are called as
middleware or middleware components. Each middleware component in the request
processing pipeline is responsible for invoking the next component in the pipeline or
short-circuiting the pipeline by not calling to the next middleware component.
Use and Run methods:
The Use and Run extension methods allow us to register the Inline Middleware
component into the Request processing pipeline. In ASP.NET Core, the Run extension
method allows us to add the terminating middleware (the middleware which will not call
the next middleware components in the request processing pipeline). The Use
extension method allows us to add the middleware components which may call the next
middleware component in the request processing pipeline.
If you observe the above Configure method, then you will see that it gets an instance of
the IApplicationBuilder interface and using this IApplicationBuilder instance along
with the extension methods such as Use and Run, we can configure the Middleware
components.
As you can see in the above configure method, two middleware components are
registered in the request pipeline using the IApplicationBuilder instance i.e. app. They
are as follows
The second middleware component is registered by using the Run() extension method.
As this component is registered using the Run extension method, so it is going to be a
terminating middleware component means it will not call the next middleware
component in the request processing pipeline. This middleware component is simply
going to write the “Hello World!” message on to the Response object. The points that
you need to remember is, this is the component which will respond to each and every
incoming HTTP request at the moment.
At the moment if you run the application, then it will give you “Hello World!” as output
irrespective of the URL Path. That means all the incoming requests are handled by this
middleware component only.
As you can see from the definition of the Run() method, it is implemented as an
extension method of IApplicationBuilder interface. This is the reason why we are able to
call the Run() method using IApplicationBuilder instance i.e. app. If you are new to the
extension method then please read the below article where we discussed the extension
methods in detail.
https://dotnettutorials.net/lesson/extension-methods-csharp/
You can also see from the above image that the Run() method takes an input parameter
of type RequestDelegate. Following is the definition of RequestDelegate.
From the above definition, we can see that the RequestDelegate is a delegate which
takes an input parameter of type HttpContext object.
If you are new to delegates then I strongly recommended you to read the following
article where we discussed the delegates in detail.
https://dotnettutorials.net/lesson/delegates-csharp/
In our example, we are passing the request delegate inline as an anonymous method
using lambda expression and moreover we passing the HTTPContext object as an input
parameter to the request delegate. The following diagram shows the above
Note: Instead of passing the request delegate inline as an anonymous method, you can
also define the request delegate in a separate class and pass it here which we will
discuss in a later article.
Example:
Let us create another middleware using the Run() extension method. To do this, modify
the Configure method of the Startup class as shown below.
Now we have two middleware components registered with the Run() extension method.
If you run the application then you will get the following output.
The output is coming from the first middleware component. The reason is, when we
registered a middleware component using the Run() extension method then that
component becomes a terminal component means it will not call to the next middleware
component in the request processing pipeline.
Then the question that comes to your mind is how to call the next component in the
request processing pipeline, the answer is registered your middleware component using
the Use extension method as shown below.
Now run the application and you will see the output as expected which is coming from
both the middleware components.
Understanding the Use extension method:
The Use extension method adds a middleware delegate defined in-line to the
application’s request pipeline. Following is the definition of the Use extension method:
As you can see this method is also implemented as an extension method on the
IApplicationBuilder interface. This is the reason why we are able to invoke this method
using the IApplicationBuilder instance. As you can see from the above definition, this
method takes two input parameters. The first parameter is the HttpContext context
object through which it can access both the HTTP request and response. The second
parameter is the Func type i.e. it is a generic delegate which can handle the request or
call the next middleware component in the request pipeline.
In the next article, I am going to discuss the ASP.NET Core request processing
pipeline with an example.
SUMMARY
To understand this concept, let us modify the Configure() method of the Startup class as
shown below. Here we are registering three middleware components into the request
processing pipeline. As you can see first two middleware components are registered
using the Use() extension method and the last one is registered using the Run()
extension method. In our previous article, we discussed when and how to use Run
and Use extension methods in details.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FirstCoreWebApplication
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
The CreateDefaultBuilder() method of the Program class which is called by the Main()
method configures the logging. As ASP.NET Core is open source, so you can verify this.
In order to verify this go to the Source code of WebHost class by visiting the following
link
https://github.com/aspnet/MetaPackages/blob/release/2.2/src/Microsoft.AspNetCore/We
bHost.cs
Somewhere within the code, you will find the ConfigureLogging() method as shown
below
As shown in the above image, the logger is configured for Console, Debug and
EventSource. In our example, we are using the logger instance which is provided
through Dependency Injection to log the information.
If you are running your application using .NET Core CLI, then you can see the logged
information on the Console window whereas if you running your application directly from
Visual Studio, then you can see the logged information in the output window.
So run the application, now select the Debug from the drop-down list of the Output
window as shown below to see the logged information.
Here you need to keep the focus on the order on which it logged information. It logged
the information in the following order.
When the incoming HTTP request comes, first it receives by the first middleware
component i.e. Middleware1 which logs “Middleware1: Incoming Request” so as a
result first we see this message first on the output window.
Once the first middleware logs the information, then it calls next() method which will
invoke the second middleware in the request processing pipeline i.e. Middleware2.
The third middleware handles the request and then produces the response. So, the third
information that we see in the output window is “Middleware3: Incoming Request
handled and response generated”.
Each middleware component can perform some operations before and after invoking
the next component using the next delegate. A middleware component can also decide
not to call the next middleware component which is called as short-circuiting the
request pipeline.
The middleware component in asp.net core has access to both the incoming request
and the outgoing response.
The most important point that you need to keep in mind is the order in which the
middleware components are added in the Configure method of the Startup class defines
the order in which these middleware components are going to be invoked on requests
and the reverse order for the response. So, the order is critical for defining security,
performance, and functionality of the application.
In the next article, I am going to discuss how to handle the static files in ASP.NET
Core using Static Files middleware components.
SUMMARY
In this article, I try to explain the ASP.NET Core Request Processing Pipeline with an
example. I hope this article will help you with your need. I would like to have your
feedback. Please post your feedback, question, or comments about this article.
In ASP.NET Core, the Request delegates are used to build the request pipeline. That
means the request delegates are used to handle each incoming HTTP request. You can
configure the Request delegates using the Run, Map, and Use extension methods. You
can specify an individual request delegate using an in-line anonymous method which is
also called as in-line middleware, or you can specify the request delegates using a
reusable class. These reusable classes and in-line anonymous methods are called as
middleware or middleware components. Each middleware component in the request
processing pipeline is responsible for invoking the next component in the pipeline or
short-circuiting the pipeline by not calling to the next middleware component.
Use and Run methods:
The Use and Run extension methods allow us to register the Inline Middleware
component into the Request processing pipeline. In ASP.NET Core, the Run extension
method allows us to add the terminating middleware (the middleware which will not call
the next middleware components in the request processing pipeline). The Use
extension method allows us to add the middleware components which may call the next
middleware component in the request processing pipeline.
If you observe the above Configure method, then you will see that it gets an instance of
the IApplicationBuilder interface and using this IApplicationBuilder instance along
with the extension methods such as Use and Run, we can configure the Middleware
components.
As you can see in the above configure method, two middleware components are
registered in the request pipeline using the IApplicationBuilder instance i.e. app. They
are as follows
The second middleware component is registered by using the Run() extension method.
As this component is registered using the Run extension method, so it is going to be a
terminating middleware component means it will not call the next middleware
component in the request processing pipeline. This middleware component is simply
going to write the “Hello World!” message on to the Response object. The points that
you need to remember is, this is the component which will respond to each and every
incoming HTTP request at the moment.
At the moment if you run the application, then it will give you “Hello World!” as output
irrespective of the URL Path. That means all the incoming requests are handled by this
middleware component only.
https://dotnettutorials.net/lesson/extension-methods-csharp/
You can also see from the above image that the Run() method takes an input parameter
of type RequestDelegate. Following is the definition of RequestDelegate.
From the above definition, we can see that the RequestDelegate is a delegate which
takes an input parameter of type HttpContext object.
If you are new to delegates then I strongly recommended you to read the following
article where we discussed the delegates in detail.
https://dotnettutorials.net/lesson/delegates-csharp/
In our example, we are passing the request delegate inline as an anonymous method
using lambda expression and moreover we passing the HTTPContext object as an input
parameter to the request delegate. The following diagram shows the above
Note: Instead of passing the request delegate inline as an anonymous method, you can
also define the request delegate in a separate class and pass it here which we will
discuss in a later article.
Example:
Let us create another middleware using the Run() extension method. To do this, modify
the Configure method of the Startup class as shown below.
Now we have two middleware components registered with the Run() extension method.
If you run the application then you will get the following output.
Getting Response from 1st Middleware
The output is coming from the first middleware component. The reason is, when we
registered a middleware component using the Run() extension method then that
component becomes a terminal component means it will not call to the next middleware
component in the request processing pipeline.
Then the question that comes to your mind is how to call the next component in the
request processing pipeline, the answer is registered your middleware component using
the Use extension method as shown below.
Now run the application and you will see the output as expected which is coming from
both the middleware components.
The Use extension method adds a middleware delegate defined in-line to the
application’s request pipeline. Following is the definition of the Use extension method:
As you can see this method is also implemented as an extension method on the
IApplicationBuilder interface. This is the reason why we are able to invoke this method
using the IApplicationBuilder instance. As you can see from the above definition, this
method takes two input parameters. The first parameter is the HttpContext context
object through which it can access both the HTTP request and response. The second
parameter is the Func type i.e. it is a generic delegate which can handle the request or
call the next middleware component in the request pipeline.
In the next article, I am going to discuss the ASP.NET Core request processing
pipeline with an example.
SUMMARY