Create your own custom middleware in ASP.NET Core
You've heard of middleware in ASP.NET Core, but don't know what it is. This tutorial will show you what it is, or you can watch the video:
Download the code example
You'll need to fill out the code example form. We will send you an email where you can download the code example.
Software
This is the software that will need to be installed onto your machine.
Visual Studio 2026. Version 18.0.0 or above. It will work with the free community version.
.NET 10 SDK. Version 10.0.0 or above.
The application
Open up RoundTheCode.Middleware.slnx in Visual Studio.
A custom middleware class has been added in RequestLoggingMiddleware.cs. This returns a 503 Service Unavailable response for all requests if the MaintenancePage property is set to true.
It also fires off a background task once the response is completed. It will wait 5 seconds before myScopedService.Test() is called.
You can see this by running the application. If MaintenancePage is set to false, it will load the Swagger documentation at https://localhost:7170/swagger. Open up the Console window and see that myScopedService.Test() has been called for each request.
If MaintenacePage is set to true, https://localhost:7170/swagger will just output "This page is currently unavailable".
Download this code example
Enter your email and we'll send you a download link.
Latest code examples
Download a .NET 11 Web API with Minimal APIs that uses FullJoin, MinBy, MaxBy, GroupBy and split queries in EF Core 11, with the SQL each query generates.
See a .NET 11 app that has Microsoft's Learn MCP server integrated it, so Claude Code is able to search the Microsoft documentation as well as reading articles and code examples.
Download this code example
Enter your email and we'll send you a download link.