asp.netkentico-13

StatusPagesWithReexecute does not work with UseExceptionHandler


In my Kentico 13 website, I'm trying to use an ErrorController to display custom error pages in response to http error codes. The error controller works with:

app.UseStatusCodePagesWithReExecute("/error/{0}");

but it doesn't if the page erroring throws an exception instead.

I tried using app.UseExceptionHandler like this:

app.UseExceptionHandler(new ExceptionHandlerOptions
    {
        AllowStatusCode404Response = true,
        ExceptionHandlingPath = "/error/500"
    });

And that causes exceptions to be handled by the ErrorController, but then, if a controller returns 404, it's no longer handled by the ErrorController, and instead you leave the site entirely and get the generic browser page saying a 404 was returned. I don't know how to make these two middlewares work with each other.

I tried to use AllowStatusCode404Response = false, as well, but it didn't work.


Solution

  • Solved it, the call to UseStatusCodePagesWithReExecute needs to go before calling UseRouting. That way, I can use UseExceptionHandler without 404s going unhandled by the ErrorController. It's worth noting that if you use UseExceptionHandler, the controller will need to have logic to log the exception, else, it just gets swallowed.

    EDIT: Actually, no, exception still ends up in the Kentico Event Log without the ErrorController having any logic to do that.