Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Middleware

Introduction

Middleware is software that is assembled into an application pipeline to handle requests and responses in ASP.NET Core. Each component chooses whether to pass the request to the next component in the pipeline, and can perform operations before and after the next component is invoked.

Setting Up the Development Environment

Before you start, ensure you have the following installed:

  • Visual Studio 2019 or later
  • .NET Core SDK

Example: Installing .NET Core SDK

https://dotnet.microsoft.com/download/dotnet-core

Creating a New ASP.NET Core Project

Open Visual Studio and create a new project:

  • Select "Create a new project".
  • Choose "ASP.NET Core Web Application" and click "Next".
  • Name your project and select the location to save it. Click "Create".
  • In the "Create a new ASP.NET Core Web Application" dialog, select "Web Application (Model-View-Controller)" and click "Create".

Understanding Middleware

In ASP.NET Core, middleware components are assembled into an application pipeline to handle requests and responses. The middleware components are executed in the order they are added to the pipeline.

Creating Custom Middleware

To create custom middleware, you need to create a new class and implement the Invoke or InvokeAsync method. Here’s an example of a simple custom middleware that logs the request processing time:

Example: Custom Middleware

public class RequestTimingMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext context)
    {
        var startTime = DateTime.UtcNow;
        context.Response.OnStarting(() =>
        {
            var processingTime = DateTime.UtcNow - startTime;
            context.Response.Headers["X-Processing-Time-Milliseconds"] = processingTime.TotalMilliseconds.ToString();
            return Task.CompletedTask;
        });

        await _next(context);
    }
}

Registering Middleware

To use the custom middleware, you need to register it in the Startup.cs file using the IApplicationBuilder interface. Here’s how you can do it:

Example: Registering Middleware in Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    // Register custom middleware
    app.UseMiddleware<RequestTimingMiddleware>();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Built-in Middleware

ASP.NET Core comes with several built-in middleware components that can be used to handle common tasks such as routing, authentication, and error handling. Here are some examples:

Example: Using Built-in Middleware

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Middleware Order

The order in which middleware components are added to the pipeline is crucial. Middleware added earlier in the pipeline can run before and after the middleware added later. Here’s an example to demonstrate this:

Example: Middleware Order

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.Use(async (context, next) =>
    {
        // Do work before the next middleware
        await next.Invoke();
        // Do work after the next middleware
    });

    app.Use(async (context, next) =>
    {
        // Do work before the next middleware
        await next.Invoke();
        // Do work after the next middleware
    });

    app.Run(async context =>
    {
        await context.Response.WriteAsync("Hello from the last middleware!");
    });
}

Exception Handling Middleware

Exception handling is a common requirement in web applications. ASP.NET Core provides built-in middleware to handle exceptions globally. Here’s how you can add exception handling middleware to your application:

Example: Exception Handling Middleware

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Running the Application

Run the application by pressing F5 or clicking the "Run" button in Visual Studio. Navigate to https://localhost:5001/ to see your middleware in action. You can check the response headers to see the custom processing time header added by the middleware.

Conclusion

Congratulations! You have learned how to create and register custom middleware in ASP.NET Core. This tutorial covered the basics of setting up a new project, creating custom middleware, using built-in middleware, and understanding the order of middleware. From here, you can extend your application with more complex middleware components to handle various tasks.