Ninject 和 MVC3 控制器和操作上的依赖注入操作过滤器存在问题

发布于 2024-11-04 19:47:48 字数 976 浏览 0 评论 0原文

最近,我决定删除控制器中的一堆操作级别过滤器,并用单个控制器级别过滤器替换它们。

现在我收到此错误消息。

Error activating LogActionFilter
More than one matching bindings are available.
Activation path:
 1) Request for LogActionFilter

Suggestions:
 1) Ensure that you have defined a binding for LogActionFilter only once.

我确信该错误与操作过滤器被绑定两次有关,因为这就是我所做的更改。但是,当我在此处查看文档时,我可以看到它指定/执行相同的操作。所以我真的不确定我做错了什么。

我的示例控制器

[LogAction]
public class SomeController : Controller
{
    public ActionResult SomeAction()
    { 

    }
}

我的注册码

public static void RegisterFilters()
{
    Kernel.BindFilter<LogActionFilter>(FilterScope.Controller, 0)
    .WhenControllerHas<LogActionAttribute>();

    Kernel.BindFilter<LogActionFilter>(FilterScope.Action, 0)
        .WhenActionMethodHas<LogActionAttribute>();
}

Recently I decided to remove a heap of action level filters in a controller and replace them with a single controller level filter.

Now I'm getting this error message.

Error activating LogActionFilter
More than one matching bindings are available.
Activation path:
 1) Request for LogActionFilter

Suggestions:
 1) Ensure that you have defined a binding for LogActionFilter only once.

I'm sure the error is related to action filter being bound twice, as that's what I've changed. However, when I view the documentation here I can see it specifies/does the same. So I'm really not sure what I'm doing wrong.

My sample controller

[LogAction]
public class SomeController : Controller
{
    public ActionResult SomeAction()
    { 

    }
}

My registration code

public static void RegisterFilters()
{
    Kernel.BindFilter<LogActionFilter>(FilterScope.Controller, 0)
    .WhenControllerHas<LogActionAttribute>();

    Kernel.BindFilter<LogActionFilter>(FilterScope.Action, 0)
        .WhenActionMethodHas<LogActionAttribute>();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

时光暖心i 2024-11-11 19:47:48

如果您的控制器及其操作之一同时具有 LogActionAttribute,就会发生这种情况。

This happens if your controller and one of its actions have the LogActionAttribute at the same time.

北风几吹夏 2024-11-11 19:47:48

(我知道答案已经被接受,但这是为了文档的目的。)

如果您只能使用发布版本,临时解决方案是创建两个子类并分别注册它们。这是我的应用程序中的一个示例:

public class MyAuthorizationFilter : IAuthorizationFilter
{
   /* call base ctor */
}

public class MyControllerAuthorizationFilter : MyAuthorizationFilter
{
   /* call base ctor */
}

public class MyActionAuthorizationFilter : MyAuthorizationFilter
{
}

然后设置过滤器绑定:

this.BindFilter<MyControllerAuthorizationFilter>(FilterScope.Controller, 0)
            .WhenControllerHas<MyAttribute>()
            .WithConstructorArgumentFromControllerAttribute<ProtectedAttribute>(/*...*/) ;

this.BindFilter<MyActionAuthorizationFilter>(FilterScope.Action, 0)
            .WhenActionMethodHas<MyAttribute>()
            .WithConstructorArgumentFromActionAttribute<ProtectedAttribute>(/*...*/) ;

确保调用正确的“WithConstructorArgumentFrom[Controller/Action]属性”方法,否则您将收到“序列没有元素”错误(我这样做了) )。

(I know the answer's already accepted but this is for the sake of documentation.)

In case you can only use the release version, a temporary solution is to create two subclasses and register them separately. Here's an example from my application:

public class MyAuthorizationFilter : IAuthorizationFilter
{
   /* call base ctor */
}

public class MyControllerAuthorizationFilter : MyAuthorizationFilter
{
   /* call base ctor */
}

public class MyActionAuthorizationFilter : MyAuthorizationFilter
{
}

Then setup filter bindings:

this.BindFilter<MyControllerAuthorizationFilter>(FilterScope.Controller, 0)
            .WhenControllerHas<MyAttribute>()
            .WithConstructorArgumentFromControllerAttribute<ProtectedAttribute>(/*...*/) ;

this.BindFilter<MyActionAuthorizationFilter>(FilterScope.Action, 0)
            .WhenActionMethodHas<MyAttribute>()
            .WithConstructorArgumentFromActionAttribute<ProtectedAttribute>(/*...*/) ;

Make sure to call the correct 'WithConstructorArgumentFrom[Controller/Action]Attribute method or you'll get a 'Sequence has no elements' error (I did).

暗地喜欢 2024-11-11 19:47:48

更好的解决方法。事实上,我也在新版本中使用了它,而不是为控制器和操作设置两个绑定。

this.BindFilter<MyFilter>(FilterScope.Global, int.MinValue)
    .When((controllerContext, actionDescriptor) =>
                                                 controllerContext
                                                .Controller
                                                .GetType()
                                                .GetCustomAttributes(typeof(MyAttribute),true)
                                                .Length > 0 
    || actionDescriptor.GetCustomAttributes(typeof(MyAttribute), true).Length > 0);

Better workaround. In fact I use this in the new version too rather than have two bindings for controllers and actions.

this.BindFilter<MyFilter>(FilterScope.Global, int.MinValue)
    .When((controllerContext, actionDescriptor) =>
                                                 controllerContext
                                                .Controller
                                                .GetType()
                                                .GetCustomAttributes(typeof(MyAttribute),true)
                                                .Length > 0 
    || actionDescriptor.GetCustomAttributes(typeof(MyAttribute), true).Length > 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文