【发布时间】:2023-03-23 15:12:01
【问题描述】:
在我的 ASP.Net MVC 应用程序中,我实现了一个自定义 ActionFilter 来授权用户。
我使用 CastleWindsor 为所有控制器提供依赖注入,如下所示:
protected virtual IWindsorContainer InitializeServiceLocator()
{
IWindsorContainer container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
container.RegisterControllers(typeof(HomeController).Assembly);
ComponentRegistrar.AddComponentsTo(container);
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
return container;
}
在我的 CustomAttribute 中,我需要一个所有控制器都使用的依赖项,但是我无法在属性中使用基于构造函数的注入。
那么这里最干净的出路是什么?如何提供依赖?
【问题讨论】:
-
不要将行为放在属性中...
-
恕我直言,最佳答案在这里stackoverflow.com/questions/10708565/…(在这个问题本身 - 很好的例子(提示:
IFilterProvider))。
标签: c# asp.net-mvc dependency-injection castle-windsor action-filter