Learn Minimal APIs from start to finish. Perfect for beginners.

Create your own custom middleware in ASP.NET Core

David Grace David Grace

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.

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 truehttps://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.

Download this code example

Enter your email and we'll send you a download link.