In an Asp.Net Core Mvc app using .Net Core 3.0, I am trying to set up global exception handling using app.UseExceptionHandler("/Error/Show"), but the Error controller is never hit.
If I use:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
ILoggerFactory loggerFactory,
IDataAccess dataAccess,
IHttpContextAccessor httpContextAccessor)
{
app.UseExceptionHandler("/Error/Show");
...
}
The Error controller show Action never gets hit, neither does the Error controller constructor.
If I change the code to:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
ILoggerFactory loggerFactory,
IDataAccess dataAccess,
IHttpContextAccessor httpContextAccessor)
{
app.UseExceptionHandler(new ExceptionHandlerOptions
{
ExceptionHandler = async context =>
{
await context.Response.WriteAsync(Newtonsoft.Json.JsonConvert.SerializeObject(new
{
title = "An Error Occurred",
message = "The error was caught by UseExceptionHandler"
}));
}
});
...
}
The expected response is returned, but I'd really like to use the ExceptionHandlingPath so that I can return json if the request was made using ajax and a razor view if the request was a standard Get.
I also tried using:
app.UseExceptionHandler(new ExceptionHandlerOptions
{
ExceptionHandlingPath = "/Error/Show"
});
but this behaves the same as app.UseExceptionHandler("/Error/Show")
Any ideas why ExceptionHandlingPath wouldn't work, but ExceptionHandler would work as expected?
