在绑定使用MVC 3依赖注入与Ninject 2.2的区域的全球行动过滤器将所有控制器 [英] Bind a Global Action Filter to all Controllers in an Area using MVC 3 Dependency Injection with Ninject 2.2

查看:177
本文介绍了在绑定使用MVC 3依赖注入与Ninject 2.2的区域的全球行动过滤器将所有控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用ASP.NET MVC 3和Ninject 2.2注入Logger对象到一个自定义ActionFilterAttribute感谢我在这个得到的帮助<一个href=\"http://stackoverflow.com/questions/5439921/mvc-3-dependency-injection-with-ninject-2-2-global-action-filter\">post.

现在,我想我的自定义ActionFilterAttribute仅绑定是在特定区域的所有控制器。

我就能上手下面的结合,但它只能处理在一定区域内一个控制器。我想我的code绑定到所有控制器在一个特定的区域。任何想法?

  ///&LT;总结&gt;
///装入模块或在这里注册您服务!
///&LT; /总结&gt;
///&LT; PARAM NAME =内核方式&gt;内核&LT; /参数&GT;
私有静态无效RegisterServices(内核的iKernel)
{
    kernel.Bind&所述; ILogger方式&gt;()到&lt; Log4NetLogger方式&gt;()InRequestScope();
    kernel.BindFilter&LT; TestLoggingAttribute&GT;(
        FilterScope.Controller,0)
            .WhenControllerType&LT; OrganizationController&GT;();
}


解决方案

这帮助了我,谢谢达林。然而,context.RouteData.Values​​没有足够的面积我,但context.RouteData.DataTokens [区]没有!也是在我的情况下,我不得不控制器,都不在特定领域(如共享控制器),因此我不得不检查datatoken区为空。这是对我工作:

 内核
   .BindFilter&所述; TestLoggingAttribute&GT;(FilterScope.Controller,0)
   。当((背景下,广告)=&GT; context.RouteData.DataTokens [区] = NULL&放大器;!&安培; context.RouteData.DataTokens [区] ==本组织);

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>();
}

解决方案

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:

kernel
   .BindFilter<TestLoggingAttribute>(FilterScope.Controller, 0)
   .When((context, ad) => context.RouteData.DataTokens["area"] != null && context.RouteData.DataTokens["area"] == "Organization");

这篇关于在绑定使用MVC 3依赖注入与Ninject 2.2的区域的全球行动过滤器将所有控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆