autofac mvc3 属性注入
我正在从事 asp.net mvc3 项目。我正在使用 autofac 进行 DI。 我有一个属性说
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);
}
}
所以我需要将我的依赖项 IUserContext 注入到该属性中。 我将如何实现它。我在actionfiterprovider上到处找到了代码, 但找不到任何完整的东西。
帮助将不胜感激。
问候
帕明德
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Alex Meyer-Gleaves 在 Autofac wiki 上的文章: http://code .google.com/p/autofac/wiki/Mvc3Integration#Filter_Attribute_Property_Injection
您需要调用
RegisterFilterProvider()
扩展方法:这将根据需要将属性注入到过滤器属性中(据我所知,您不需要向 Autofac 注册属性类型即可使其工作。)
希望这会有所帮助,
Nick
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