Associating a global action filter with all controllers in an area using MVC 3 Injection Dependency Injection with Ninject 2.2

I managed to use ASP.NET MVC 3 and Ninject 2.2 to insert a registrar object into a custom ActionFilterAttribute, thanks to the help I received in the post.

Now I would like to bind my own ActionFilterAttribute only to all controllers located in a specific area.

I managed to start with the next binding, but it only processes one controller in a specific area. I want 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>(); } 
+4
source share
2 answers

It helped me, thanks Darin. However, context.RouteData.Values ​​did not have an area for me, but context.RouteData.DataTokens ["area"] did! also in my case, I had a controller that was not in certain areas (e.g. shared controllers), so I had to check that the data area was zero. 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"); 
+6
source
 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")); 
+5
source

Source: https://habr.com/ru/post/1345789/


All Articles