Failwyn
Failwyn

Reputation: 817

UseExceptionHandler is not working with ExceptionHandlingPath

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?

Upvotes: 2

Views: 4606

Answers (2)

Failwyn
Failwyn

Reputation: 817

It turns out the issue was that attempting to inject the IExceptionHandlerPathFeature into the Error Controller caused the ExceptionHandler to fail silently. After removing the IExceptionHandlerPathFeature parameter from the Error Controller constructor, the handler started working as expected.

public async Task<IActionResult> Show()
{
  var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
}

Upvotes: 1

Fei Han
Fei Han

Reputation: 27793

set up global exception handling using app.UseExceptionHandler("/Error/Show"), but the Error controller is never hit.

I did a test with using same code to configure a custom error handling page, like below, which works as expected.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseExceptionHandler("/Error/Show");  

    app.UseStaticFiles();

    app.UseRouting();


    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");

    });
}

Error controller and Show action

public class ErrorController : Controller
{
    public IActionResult Show()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

Test Result

enter image description here

To troubleshot the issue, please try:

1) make sure you create corresponding "/Error/Show" controller action and view

2) check if you enabled the Developer Exception Page app.UseDeveloperExceptionPage();

3) check if you add other middlewares/filters, which cause the issue.

Upvotes: 1

Related Questions