How many ways can you create an ASP.NET Core Middleware?

Sriram Kumar Mannava
3 min readApr 10, 2023

First of all, What is a Middleware?

A Middleware is a small block of independent code that runs during a request pipeline. A Request pipeline is a series of steps that happen before a request reaches its intended endpoint.

How does a Middleware work?

A typical Middleware component has two parameters, the HttpContext which contains the current Request context and a RequestDelegate which chains to the next Middleware to be executed in the pipeline.

Every Middleware must call the next() method at the end of its implementation. Otherwise the next Middleware is not called. This scenario is called Short-circuiting.

These Middleware components are executed one after the other in the same sequence in which they are added to the pipeline. Once the request passes through all the middlewares in the pipeline and a response is generated by the application, it passes through all these Middleware components once again before being sent out to the network.

In .NET, you will add all pre-built functionality to your application such as Authentication, Authorization, Endpoints, Response Caching, Exception Handling etc. as middlewares to be added to the request pipeline!

Different ways you can create a Middleware

In .NET, you can create and add a Middleware to the request pipeline in more than one ways.

All the middlewares are added to the IApplicationBuilder object, which is a part of the WebApplication built using the WebApplication.Build() method.

.NET provides us with extension methods on top of this interface to add any default or custom middleware components.

The below are the ways you can do it -

1. Use method -

Use() method is available in the IApplicationBuilder interface. This method takes a Function delegate, with two parameters — HttpContext and RequestDelegate. You can just call this method and within the Function delegate write your code inside it.

2. UseWhen method -

UseWhen() method is a logical extension of the Use() method. This method takes two parameters — a boolean condition and a Function delegate. You use this variation to execute a Middleware component only when a specified condition is met, such as a Request containing some path parameters or any query string conditions.

3. Run method -

Run() method also runs a Middleware code over the request pipeline. But the difference is that the request stops moving forward after Run() method is executed. This method is specifically used for “Short Circuiting” scenarios where the request need not be processed after a step.

4. UseMiddleware method -

UseMiddleware() method doesn’t take any parameters. Instead it takes a type parameter of a class that runs a Middleware code. You use this variant when you need to create a Middleware component which is large enough to be separated and encapsulated into its own method.

Different Ways of creating a Middleware in ASP.NET Core

Checkout my new ebook Exploring ASP.NET Core Middlewares — A Complete Guide for Developers — I will explain everything you need to know about working with Middleware components in ASP.NET Core!!😁

--

--

Sriram Kumar Mannava
Sriram Kumar Mannava

Written by Sriram Kumar Mannava

I make Full Stack Development Easy for You | Full Stack .NET Dev | 3× AWS Certified | Blogger

No responses yet