How to Implement Custom Middleware in ASP.NET Core etd_admin, May 10, 2025May 10, 2025 In ASP.NET Core, middleware is a fundamental concept that enables developers to handle HTTP requests and responses as they flow through the application pipeline. Sometimes, built-in middleware isn’t enough, and you’ll need to implement custom middleware in ASP.NET Core to meet specific application requirements like logging, authentication, or request modification. This guide will show you how to implement custom middleware in ASP.NET Core with a simple example and explanation. What Is Middleware? Middleware is software that’s assembled into an application pipeline to handle requests and responses. Each middleware component: Receives an incoming HTTP request. Can perform actions before and after calling the next middleware in the pipeline. Passes the request to the next component or short-circuits the pipeline. Why Create Custom Middleware? You may want to create custom middleware when: You need to inject custom logic into the request pipeline (e.g., custom logging, headers, or response formatting). You want reusable and testable request/response handling logic. Step-by-Step: Implement Custom Middleware in ASP.NET CoreL Let’s go through a practical example of custom middleware that logs request processing time. Create a Middleware Class public class RequestTimingMiddleware { private readonly RequestDelegate _next; public RequestTimingMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { var startTime = DateTime.UtcNow; // Call the next middleware in the pipeline await _next(context); var duration = DateTime.UtcNow - startTime; Console.WriteLine($"Request took {duration.TotalMilliseconds} ms"); } } Register Middleware in Startup.cs or Program.cs In older versions (like .NET Core 3.1), you would do this in the Configure method inside Startup.cs: var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); // Use custom middleware app.UseMiddleware<RequestTimingMiddleware>(); app.MapGet("/", () => "Hello World!"); app.Run(); In older versions (like .NET Core 3.1), you would do this in the Configure method inside Startup.cs: public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseMiddleware<RequestTimingMiddleware>(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } Test the Middleware Run your application and make a request (e.g., to /). You’ll see console output like: Request took 2.567 ms This confirms that your middleware has intercepted the request, executed logic, and passed control correctly. Knowing how to implement custom middleware in ASP.NET Core helps you gain fine-grained control over how requests and responses are handled in your application. It’s a clean and powerful way to extend the behavior of your web app with reusable components. Whether it’s logging, security checks, or modifying headers, custom middleware can keep your code organized and testable. The ability to implement custom middleware in ASP.NET Core is a key skill for any backend .NET developer working with scalable and maintainable web applications. .NET .NETASP .NET