The world of .NET Core is not new to most of us. We have been hearing about it since last few years and many of us already started using it.
ASP .NET Core web applications have concept of middleware. A middleware intercepts HTTP requests sent to .NET Core web application. In this article, let’s have a look at different ways to create custom middleware in an ASP .NET Core Web Application.
Create Web App
For this demo, I am selecting
Empty option from the list. Because mostly we will be playing around the
Alternatively, you can use below script to create the empty ASP .NET Core web application using .NET CLI .
What is Middleware ?
Before proceeding to create a new middleware, let’s first understand what is a request delegate and what is a middleware.
As per documentation, a Middleware is a piece of code that resides in the applications pipeline to handle requests and responses.
So, every middleware can perform below actions:
- process incoming intercept HTTP requests,
- decide if the request should be handed over to the next middleware or just end the response
- process outgoing responses
Request Delegate vs Middleware
Request delegate can then use either inline anonymous method or a reusable class for processing requests and responses. This inline anonymous method (in-line middleware) or reusable class is called as middleware or middleware component.
The middlewares are configured in
Configure method of
Startup.cs. For any real world site, there can be one or more than one middlewares configured there.
The .NET Core framework has default implementations for many common middlewares and when you create a ASP .NET Core web project, they are automatically configured in
Startup.cs. Some common examples of middleware are
- Authorization and Authentication
- Static Files Middleware
- Exception Handling Middleware
- Routing Middleware
If you want to add any custom logic in the application pipeline, you can do that by writing custom middleware. As stated earlier, there are three different ways to configure middleware, Run, Map, and Use extension methods. Let’s see how to use them.
So, in the newly created web application project, go to the
Startup.cs file and delete everything from Configure method. Using Run extension method, you can easily write simple inline middleware as shown below:
The above middleware code just return the same response to every request. Note, that there is no provision to call next middleware. If another
Run delegate is added after the
Run delegate, it’s not called.
The use method for registering the request delegate provides an additional parameter – namely next. Using this parameter, the next request delegate from the pipeline can be invoked.
Below is the example of how it works.
Please note that writing anything to response is not allowed in the execution flow which invokes next middleware.
For ex, let’s say you have a middleware which checks if user has right to allow specific operation. The middleware can have an IF block to check the rights and stop the flow if the user does not have right. When the middleware decides to stop the flow, it should either redirect user to some error page without running next middleware. The same middleware can decide to invoke next middleware if user has expected rights.
If the middleware tries to change any contents of response in the same execution flow which invokes next middleware, there are chances that there might be corrupted HTML body or there might be a protocol violation errors as per documentation.
Measure Execution Interval
Below is a simple example of how middleware can be used to log and understand how much time is taken for a request processing. It assigns a unique identifier to every incoming request and uses Stopwatch class to count how much time was taken by the application to process one request.
It logs start of request processing and end of request processing events to a file.
There is one more extension method – Map – which we have not discussed in this article. I will write about it in the next article.
I hope this information was useful for you. Let me know your thoughts.