Ninject 和 MVC3 控制器和操作上的依赖注入操作过滤器存在问题
最近,我决定删除控制器中的一堆操作级别过滤器,并用单个控制器级别过滤器替换它们。
现在我收到此错误消息。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的控制器及其操作之一同时具有 LogActionAttribute,就会发生这种情况。
This happens if your controller and one of its actions have the LogActionAttribute at the same time.
(我知道答案已经被接受,但这是为了文档的目的。)
如果您只能使用发布版本,临时解决方案是创建两个子类并分别注册它们。这是我的应用程序中的一个示例:
然后设置过滤器绑定:
确保调用正确的“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:
Then setup filter bindings:
Make sure to call the correct 'WithConstructorArgumentFrom[Controller/Action]Attribute method or you'll get a 'Sequence has no elements' error (I did).
更好的解决方法。事实上,我也在新版本中使用了它,而不是为控制器和操作设置两个绑定。
Better workaround. In fact I use this in the new version too rather than have two bindings for controllers and actions.