.Net Core IFilter inherited interfaces wont't execute any methods

461 Views Asked by At

I have a class inheriting from ActionFilterAttribute and IActionFilter. Now I'm implementing the interface methods and applied the filter to the controller and action too. Nothing happens ...

I ensured it by applying some breakpoints all over the filter. To clarify I'm using System.Web.Mvc.Filters and that's most of it I just want to have a filter that the action goes to before it gets executed.

public class AuthJwtFilter : ActionFilterAttribute, IActionFilter
{
    public string Realm { get; set; }

    void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
    {
        var request = true;
    }
}

And just a break point on var request to see if it gets in.

[AuthJwtFilter]
    [HttpGet("/test")]
    public IActionResult Test()
    {
        return Ok("Hello");
    }

And that is the Controller Action

1

There are 1 best solutions below

6
JuanR On

Remove the IActionFilter interface from the class declaration. ActionFilterAttribute already implements that interface.

Override the base method:

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    var request = true;
}

That should work.

UPDATE: By the way, if you want your filter to fire before the action executes, you should be overriding OnActionExecuting instead.