Reputation: 11
I'm having trouble using IExceptionHandler in my project.
Do I need to install any NuGet packages?
When I try to use, Visual Studio does not find the reference. He asks if I want to create an interface named IExceptionHandler.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EventMotors.Controllers
{
public class ErrorController : Controller
{
private readonly IExceptionHandler _exceptionHandler;
public ErrorController(IExceptionHandler exceptionHandler)
{
_exceptionHandler = exceptionHandler;
}
}
}
Upvotes: 1
Views: 3156
Reputation: 19421
IExceptionHandler
interface was added in .NET 8.
Here is the blog post of anocconcuing it.
And here is another blog post explain it.
Here is a summary of the blog post
Multiple can be added, and they’ll be called in the order registered.
If an exception handler handles a request, it can return true to stop processing.
If an exception isn’t handled by any exception handler, then control falls back to the old behavior and options from the middleware.
Upvotes: 0
Reputation: 27538
IExceptionHandler
is not in asp.net core . In error action , you can use IExceptionHandlerPathFeature
to access the exception and the original request path in an error handler controller or page:
var exceptionHandlerPathFeature =
HttpContext.Features.Get<IExceptionHandlerPathFeature>();
if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
{
ExceptionMessage = "File error thrown";
}
if (exceptionHandlerPathFeature?.Path == "/index")
{
ExceptionMessage += " from home page";
}
Please refer to document :
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-3.1
Upvotes: 2