Middleware in .NET Core with Example

In this article, we will look into details about Middleware in .NET Core.

Middleware in .NET Core are similar to HttpHandlers and HttpModules which were part of classic ASP.NET. When a user hit the request from client such as browser then before it hits to the controller, it has to pass through multiple Http Process and here Middleware comes into the picture.

What is Middleware?

Middleware is component or piece of code that is written into an application pipeline to handle requests and response.

A middleware internally can pass the request to the next available middleware in the pipeline. Middleware can also perform task before and after a component in the pipeline. So basically, Http pipeline is the combination of one or more middleware. Each and every request to the application pass through the Http Pipeline.

Understand Middleware with an example

You can think of the joint in the water supply pipe as a middleware. Every flow of water is routed through a joint, just as any request and response in the application travels through middleware.

How to implement Middleware in .NET Core?

In this section, we will see about Middleware in .NET Core.

In .NET Core, we can configure Http Pipelines in Configure() method of Startup.cs. We can have multiple middleware in one .Net Core application.

You can understand middleware concept with the help of below diagram.

Middleware in .NET Core

Let’s write C# code to demonstrate Middleware in .NET Core

To demonstrate Middleware, I am using VS 2019 with .NET 5.

To implement Middleware in .NET Core, we can use below methods-

  • Use() – It adds a middleware to the application’s request pipeline.
  • Next() – Method which is use to invoke next available middleware.
  • Run() – Adds a terminal middleware to the application’s request pipeline. Terminal middleware means there is no middleware to call next.

Open Startup.cs file and within Configure() method, write below code –

Run() method to add a terminal Middleware

Below is the example of Terminal Middleware as there is no next middleware available to invoke.

 app.Run(async (context) =>
                {
                    await context.Response.WriteAsync("First Middleware");
                     
            });

Build and run the project, you will see the output as – “First Middleware”

Use() method to add Middleware

app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("First Middleware");
                await next();
            });

You may now see that in above code, we have an extra parameter next which helps to execute next available Middleware.

Add Middleware with Use() and Run()

Below is the combined code which invoke Middleware with the help of Use() and Run() method.

 app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("First Middleware");
                await next();
            });

            app.Run(async (context) =>
                {
                    await context.Response.WriteAsync("Second Middleware"); 
            });

You may notice that with Use() there is one extra parameter called next and same has been called with keyword await. However there is no extra parameter with Run() method, this is because Run() is used as a Terminal Middleware.

Watch the demo of Middleware in .NET Core Application

Built-in Middleware in .NET Core

There are multiple built-in Middleware available in .NET Core. Some of the built-in Middleware are given in below table –

MiddlewareDescription
AuthenticationHelps in Authentication process.
AuthorizationProvide support for Authorization
CORSConfigure CORS origin Resource
Cookie PolicyTracks consent from users for storing personal data
SessionFor Managing User sessions
Static FilesSupports for serving and browsing static files
URL RewriteHelps in URL rewriting and redirection process
MVCTo process requests for MVC pages
OWINInterop with OWIN-based apps, servers, and middleware.

Some of the built-in Middleware that we get while creating .NET Core web application in Visual Studio 2019 are –

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }

Order of Middleware

It is very important to write the middleware in the right order, because the order in which we write the middleware, its gets executed in the same order.

How to create Custom Middleware in .NET Core?

Now we will see, how to create and add custom middleware in .NET Core application. Custom middleware is like common .NET Core class file.

Visual Studio 2019 gives you a readymade template to add Middleware class.

Right click on your project in Solution Explorer and click “Add New Item”. In search box type “Middleware” and you will see Middleware Class in the result.

Select Middleware Class and give a name then click Add.

Complete code of custom Middleware class file will look like this –

    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
    public class CustomMiddleware
    {
        private readonly RequestDelegate _next;

        public CustomMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public Task Invoke(HttpContext httpContext)
        {

            return _next(httpContext);
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class CustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<CustomMiddleware>();
        }
    }

Go to Startup.cs file and within Configure() method, add below line to configure custom Middleware –

app.UseCustomMiddleware();

Hope you understand the concept of Middleware in .NET Core. Please share this article among your tech group and please do like our Facebook page to join us and get latest updates.

You may subscribe to the YouTube channel for latest videos on .NET Core Technologies.

Please follow and like us:

Leave a Comment