'App.UseExceptionHandler is not hit when the request is invalid

I'm using IApplicationBuilder.UseExceptionHandler.

app.UseExceptionHandler(app =>
    {
        app.Run(async context =>
        {
            context.Response.ContentType = "application/json";

            var exceptionFeature = context.Features.Get<IExceptionHandlerFeature>();

            if (exceptionFeature != null)
            {
                var exception = exceptionFeature.Error;

                StringBuilder errorMessage = new();

                if (exception is IntegrationNotFoundException integrationNotFoundException)
                {
                    await GetResponse(context, (int)HttpStatusCode.NotFound, integrationNotFoundException.Message);
                }
                else
                {
                    await GetResponse(context, (int)HttpStatusCode.InternalServerError, exception.Message);
                }
            }
        });
    });

My Url looks like this : localhost:7149/integration/{guid}.

Whenever I send request by invalid url, e.g localhost:7149/integration/notGuid, the ExceptionHandler is not activated, an empty 404 is returned, so I am unable to return the error model that I'm using.

So the application becomes unpredictable with the responses.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source