2

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have verified all of those things, I'll try upgrading to the latest .net core and see if that has any effect.
@FeiHan, for step #2 listed above, should the developer exception page be enabled or disabled? You may want to clarify this for readers.
1

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>();
}

1 Comment

I ended up having the same issue, Controller with the endpoints for UseExceptionHandler was failing silently trying to inject an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.