Do I have to activate it elsewhere or can you help me? When I run the project, it goes straight to the controller and middleware does not run.

This is my middleware.

public class AuthenticationMiddleWare
{
    private RequestDelegate nextDelegate;
        
    public AuthenticationMiddleWare(RequestDelegate next, IConfiguration config)
    {
        nextDelegate = next;
    }
        
    public async Task Invoke(HttpContext httpContext)
    {
        try
        {
            // somecode
            await nextDelegate.Invoke(httpContext);
        
        }
        catch (Exception ex)
        {
        
        }
    }    
}

Solution 1:

Make sure you have registered your middleware in Configure method in Startup class.

public class Startup
{
    ...

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseMiddleware<AuthenticationMiddleWare>();
    }
}

Reference

The Configure method - App startup in ASP.NET Core