I have in my startup.cs file:
if (env.IsDevelopment()) {
app.UseExceptionHandler("/api/error/error-local-development");
}
else {
app.UseExceptionHandler("/api/error/error");
}
But when throw new Exception() in a controller action, the Error controller Methods are never called.
[Route("api/error")]
[ApiController]
[ApiExplorerSettings(IgnoreApi = true)]
public class ErrorController : OwnBaseController
{
public ErrorController(IApplicationUserService applicationUserService, ILogger<ErrorController> logger, IDiagnosticContext diagnosticContext) : base(applicationUserService, logger, diagnosticContext)
{
}
[Route("error")]
public IActionResult Error()
{
return Problem();
}
[Route("error-local-development")]
public IActionResult ErrorLocalDevelopment([FromServices] IWebHostEnvironment webHostEnvironment)
{
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
return Problem(
detail: context.Error.StackTrace,
title: context.Error.Message);
}
}
Why is this not working?

UseExceptionHandlercalls before or after theUseRoutingandUseEndpoints?Configure.