Ken*_*rdt 4 dependency-injection ninject asp.net-mvc-areas action-filter asp.net-mvc-3
我能够使用ASP.NET MVC 3和Ninject 2.2注入Logger对象到自定义ActionFilterAttribute感谢我在此得到的帮助后.
现在我想将自定义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>();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
这帮助了我,谢谢Darin.但是,context.RouteData.Values没有我的区域,但context.RouteData.DataTokens ["area"]做了!在我的情况下,我有控制器不在特定区域(例如共享控制器),因此我必须检查datatoken区域为null.这对我有用:
kernel
.BindFilter<TestLoggingAttribute>(FilterScope.Controller, 0)
.When((context, ad) => context.RouteData.DataTokens["area"] != null && context.RouteData.DataTokens["area"] == "Organization");
Run Code Online (Sandbox Code Playgroud)
kernel
.BindFilter<TestLoggingAttribute>(FilterScope.Controller, 0)
.When((context, ad) => context.RouteData.Values["area"] == "YourAreaName");
Run Code Online (Sandbox Code Playgroud)
要么:
kernel
.BindFilter<TestLoggingAttribute>(FilterScope.Controller, 0)
.When((context, ad) => context.Controller.GetType().FullName.Contains("Areas.YourAreaName"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1754 次 |
| 最近记录: |