I have in startup
(UPDATE: the solution was to move UseRouting under the /api/error routes)
app.UseRouting();
if (env.IsDevelopment()) {
app.UseExceptionHandler("/api/error/error-local-development");
SwaggerConfig.Configure(app);
}
else {
app.UseExceptionHandler("/api/error/error");
}
app.UseCors();
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseSpaStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseRequestLocalization(options);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<ResultHub>("/hubs/resultHub");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "wwwroot";
});
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);
}
}