将依赖项注入 MVC3 过滤器
我一直在努力尝试将依赖项注入自定义授权过滤器。
OutletService(这是我试图注入到过滤器中的服务)
public class OutletService : IOutletService
{
#region Fields
private readonly IRepository<Outlet> _outletRepository;
#endregion
#region Ctor
public OutletService(IRepository<Outlet> outletRepository)
{
_outletRepository = outletRepository;
}
#endregion
// Rest of class omitted
CustomAuthorizeAttribute(部分,此示例的名称也已更改)
public class MyAuthorizeAttribute : AuthorizeAttribute
{
private IOutletService _outletService;
private IModuleService _moduleService;
public string Action { get; set; }
public int Level { get; set; }
public MarcusAuthorizeAttribute()
{
}
[Inject]
public MyAuthorizeAttribute(IOutletService outletService, IModuleService moduleService)
{
_outletService = outletService;
_moduleService = moduleService;
}
我尝试使用这篇文章作为示例,但一旦我将其连接起来,我的路由似乎都不起作用(IIS Express 返回 401/找不到?)
注入依赖项进入 ASP.NET MVC 3 操作过滤器。这种方法有什么问题吗?
如果有人有任何想法或建议,我将不胜感激! (这简直让我抓狂了!)
谢谢!
I've been having a heck of a time trying to get dependencies injected into a custom authorization filter.
OutletService (this is a service I'm trying to inject into my filter)
public class OutletService : IOutletService
{
#region Fields
private readonly IRepository<Outlet> _outletRepository;
#endregion
#region Ctor
public OutletService(IRepository<Outlet> outletRepository)
{
_outletRepository = outletRepository;
}
#endregion
// Rest of class omitted
CustomAuthorizeAttribute (partial, name changed for this example also)
public class MyAuthorizeAttribute : AuthorizeAttribute
{
private IOutletService _outletService;
private IModuleService _moduleService;
public string Action { get; set; }
public int Level { get; set; }
public MarcusAuthorizeAttribute()
{
}
[Inject]
public MyAuthorizeAttribute(IOutletService outletService, IModuleService moduleService)
{
_outletService = outletService;
_moduleService = moduleService;
}
I tried using this post as an example, but as soon as I wire it up, none of my routes seem to work (IIS Express returns a 401/cannot find?)
Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach?
If anyone has any ideas or suggestions, I'd appreciate it! (It's literally driving me up a wall now!)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ninject 的 MVC 扩展有一种将依赖项注入过滤器的机制,即 在此处的文档中进行了描述。
Ninject's MVC extension has a mechanism for injecting dependencies into filters, which is described in the documentation here.
您可以尝试此
过滤器
确保您使用正在使用的依赖项解析器注册您的服务。
You may try this
Filter
Make sure you register your services with dependency resolver you are using.