使用 MVC 3 依赖注入和 Ninject 2.2 将全局操作过滤器绑定到区域中的所有控制器
由于我在此 帖子。
现在我想将自定义 ActionFilterAttribute 仅绑定到特定区域中的所有控制器。
我能够开始使用以下绑定,但它仅处理特定区域中的一个控制器。我希望我的代码绑定到特定区域中的所有控制器。有什么想法吗?
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ILogger>().To<Log4NetLogger>().InRequestScope();
kernel.BindFilter<TestLoggingAttribute>(
FilterScope.Controller, 0)
.WhenControllerType<OrganizationController>();
}
I was able to to use ASP.NET MVC 3 and Ninject 2.2 to inject a logger object into a custom ActionFilterAttribute thanks to the help I received in this post.
Now I would like to bind my custom ActionFilterAttribute only to all controllers that are in a specific area.
I was able to get started with the following binding but it only handles one controller in a certain area. I would like my code to bind to all controllers in a specific area. Any ideas?
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ILogger>().To<Log4NetLogger>().InRequestScope();
kernel.BindFilter<TestLoggingAttribute>(
FilterScope.Controller, 0)
.WhenControllerType<OrganizationController>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有帮助,谢谢达林。但是, context.RouteData.Values 没有适合我的区域,但 context.RouteData.DataTokens["area"] 有!同样在我的例子中,我的控制器不在特定区域(例如共享控制器),因此我必须检查数据令牌区域是否为空。这对我有用:
This helped me, Thanks Darin. However, context.RouteData.Values did not have the area for me but context.RouteData.DataTokens["area"] did! also in my case I had controller that were not in specific areas (e.g. shared controllers) therefore I had to check for datatoken area to be null. This is what worked for me:
或者:
or: