Building an ASP.Net Core (V2) MVC Site.
We are using API Versions for this.
The site has controllers delivering MVC Razor Views as well as API-style Json responses.
I am trying to use the UseExceptionHandler() method, as shown below.
When i hit an exception in one of my actions, the action gets executed again and then a blank page shown. The browser console is free from any errors too. Finally, my error action is never hit.
I have a custom exception filter in place for the API controllers, also shown below. I have commented this out, figuring it was interfering. This didn't resolve any issue.
Where am I going wrong? And how can i have my custom error page hit when there is an exception.
Exception Handler Method - This results in "/v1.0/home/error"
app.UseExceptionHandler($"/v{appSettings.Value.APIVersion}/home/error");
I have also tried, after reading that this is supposed to be the path to a view
app.UseExceptionHandler("/error");
and
app.UseExceptionHandler("/views/home/error");
The Error Action
[HttpGet("error")]
public IActionResult Error()
{
var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
ViewData["statusCode"] = HttpContext.Response.StatusCode;
ViewData["message"] = exception.Error.Message;
return View();
}
The Exception Filter (currently commented out in the startup.cs)
public override void OnException(ExceptionContext context)
{
if (context.HttpContext.Request.Path.Value.Contains("/api"))
{
// Set the response
context.ModelState.GroupErrorsIntoKey("ErrorArray");
context.ModelState.AddModelError("ErrorArray", context.Exception.Message);
context.Result = new BadRequestObjectResult(context.ModelState);
}
}
UseExceptionHandler()directly. DoesError()action hit?UseStatusCodePagesWithReExecute()- this works with 404 but gives a blank page on an exception; so very frustrating!