Search code examples
c#.netasp.net-mvcasp.net-mvc-4asp.net-mvc-routing

ActionFilterAttribute is not called


I have this custom action filter:

public class PermissionChecker: ActionFilterAttribute
{
    private int _permissionId { get; set; }
    private IUserSelectorService _userService { get; set; }

    public PermissionChecker(int permissionId)
    {
        _permissionId = permissionId;
        _userService = new UserSelectorService();
    }

    public PermissionChecker(int permissionId, IUserSelectorService userService)
    {
        _permissionId = permissionId;
        _userService = userService;
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);
    }
}

and I set it in my action:

public class HomeController : Controller
{
    [PermissionChecker(1)]
    public ActionResult Index()
    {
        return View();
    }
}

but it is not working! the code doesn't pass in onActionExecuting even in constructor of PermissionChecker.


Solution

  • The signature of your OnActionExecuting is not correct, it should have ActionExecutingContext:

    Provides the context for the ActionExecuting method of the ActionFilterAttribute class.

    public override void OnActionExecuting(ActionExecutingContext filterContext){
        base.OnActionExecuting(filterContext);
    }