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
A EF Core example showing how to use Include() to join navigation properties together, when not to use Include() and when you need to configure the join.
Download a Polly resilience example for ASP.NET Core Web API, covering retries, jitter, circuit breakers, timeouts, and logging.
Download this code example
Enter your email and we'll send you a download link.