Difference between MVC FilterAttribute and Filter

Posted by zaaaaphod on Stack Overflow See other posts from Stack Overflow or by zaaaaphod
Published on 2011-06-24T00:01:33Z Indexed on 2011/06/24 0:22 UTC
Read the original article Hit count: 173

Filed under:

I'm trying to write my own custom AuthorizationAttribute that uses DI. I'm using the MUNQ IoC provider for it's speed and have decided to use constructor injection on all my classes as opposed to post instatiation property binding (because I prefer it).

I'm trying to write a custom IFilterProvider that will use my IoC container to return requests for filters (so that I can map concrete classes using the container).

I've come up with the following.

public class FilterProvider : IFilterProvider
{
    private readonly IocContainer _container;
    public FilterProvider(IocContainer container)
    {
        _container = container;            
    }
    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var x = Enumerable.Union<Object>(_container.ResolveAll<IActionFilter>(), _container.ResolveAll<IAuthorizationFilter>());
        foreach (Filter actionFilter in x)
            yield return new Filter(actionFilter, FilterScope.First, null);
    }
}

The above code will fail during the foreach because my objects that implement IAuthorizationFilter are based on FilterAttribute and not Filter

My question is, what is the difference between Filter and FilterAttribute? I would have thought that there would have been a common link between them, unless I'm missing something.

Another deeper question is, how come there is no IFilterAttributeProvider that would support IEnumerable GetFilters(...)

Is there some other way that I should be using to resolve IAuthorizationFilter via my IoC container?

Thank you very much for your help.

Z

© Stack Overflow or respective owner

Related posts about asp.net-mvc