Search code examples
asp.net-mvcdependency-injectionstructuremap

How to set Action Filter in Filter Config with Injections MVC?


I created an ActionFilter and in this filter I Injected services as Constructor Injections. These services each implement many services themselves. when I want to set my ActionFilter in FilterConfig.cs I got errors. Obviously for Constructor which need services.

public class PermissionControlActionFilter : ActionFilterAttribute
{
    readonly IApplicationUserManager _usermanager;
    readonly IPermissionHelper _permissionhelper;
    public PermissionControlActionFilter(IApplicationUserManager usermanager, IPermissionHelper permissionHelper)
    {
        _usermanager = usermanager;
        _permissionhelper = permissionHelper;
    }...

Thanks.


Solution

  • Have a look at this article and this answer to a similar question and I think you'll be able to come right.

    The point of the article and answer is that your attribute shouldn't do the work because attributes don't have a constructor valid for DI. A filter on the other hand does and can check for the existence of a specific attribute, and if it is there, then it performs the work.