Bind a Global Action Filter to all Controllers in an Area using MVC 3 Dependency Injection with Ninject 2.2

烂漫一生 提交于 2019-12-01 06:36:53

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");
kernel
    .BindFilter<TestLoggingAttribute>(FilterScope.Controller, 0)
    .When((context, ad) => context.RouteData.Values["area"] == "YourAreaName");

or:

kernel
    .BindFilter<TestLoggingAttribute>(FilterScope.Controller, 0)
    .When((context, ad) => context.Controller.GetType().FullName.Contains("Areas.YourAreaName"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!