将依赖项注入 MVC3 过滤器

发布于 2024-12-22 20:28:49 字数 1407 浏览 0 评论 0原文

我一直在努力尝试将依赖项注入自定义授权过滤器。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-12-29 20:28:49

Ninject 的 MVC 扩展有一种将依赖项注入过滤器的机制,即 在此处的文档中进行了描述

Ninject's MVC extension has a mechanism for injecting dependencies into filters, which is described in the documentation here.

难理解 2024-12-29 20:28:49

您可以尝试此

过滤器

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    private IOutletService _outletService;
    private IModuleService _moduleService;

    public string Action { get; set; }
    public int Level { get; set; }

    public MarcusAuthorizeAttribute()
    {
       _outletService = DependencyResolver.Current.GetService<IHelloService>();
       _moduleService  = DependencyResolver.Current.GetService<IModuleService>();
    }
}

确保您使用正在使用的依赖项解析器注册您的服务。

You may try this

Filter

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    private IOutletService _outletService;
    private IModuleService _moduleService;

    public string Action { get; set; }
    public int Level { get; set; }

    public MarcusAuthorizeAttribute()
    {
       _outletService = DependencyResolver.Current.GetService<IHelloService>();
       _moduleService  = DependencyResolver.Current.GetService<IModuleService>();
    }
}

Make sure you register your services with dependency resolver you are using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文