I am converting a project from a Framework 4.7.2 into a Net (Core) 6. The framework used a ExceptionFilterAttribute to call a method on the controller that caused the exception. Im just trying to port across and then refactor new ways later.
I want to get access to a method on the controller that the exception occurred on.
In the framework version the code looked like:
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
LoggingController = actionExecutedContext.ActionContext.ControllerContext.Controller as ILoggingController;
if (LoggingController == null)
{
throw new InvalidOperationException($"{GetType().Name} can can only be used on controllers that implement ILoggingController");
}
LoggingController.MethodToCall()
....
}
but when using Net 6.0 things have changed and I can't see where to get access to the Controller which called it. I can get the descriptor but don't know how that helps.
using Microsoft.AspNetCore.Mvc.Filters;
public class ExcFilterAttribuite: ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
var controller = context.ControllerContext.Controller; // Doesnt work
var endpoint = context.HttpContext.GetEndpoint();
var controllerActionDescriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>();
base.OnException(context);
}
}
How can I get to the controller from the ExceptionFilterAttribute in Net 6 ?
Here is a whole working demo below for how to invoke the controller action in custom ExceptionFilterAttribute: