I working on asp.net mvc3 project. I am using autofac for DI.
I have an attribute Say
public class MustBeLoggedInAttribute : ActionFilterAttribute
{
private IUserContext Context {get;set;}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Context.IsLoggedIn)
filterContext.Result = new RedirectResult("/users/logon");
base.OnActionExecuting(filterContext);
}
}
So I need to inject my dependency IUserContext to this attribute.
How would I achive it. I found codes here and there on actionfiterprovider,
but couldnt find anything complete.
Help will be appreciated.
Regards
Parminder
From Alex Meyer-Gleaves’ write-up on the Autofac wiki: http://code.google.com/p/autofac/wiki/Mvc3Integration#Filter_Attribute_Property_Injection
You need to call the
RegisterFilterProvider()extension method:This will inject properties into filter attributes as required (as far as I can tell, you don’t need to register the attribute types with Autofac for this to work.)
Hope this helps,
Nick