在 Ninject 中使用无参数控制器构造函数?
我在我的应用程序中使用 Ninject 进行依赖注入。这是我的控制器之一的示例:
public class DepartmentsController : Controller
{
private IDepartmentsRepository departmentsRepository;
public DepartmentsController(IDepartmentsRepository departmentsRepository)
{
this.departmentsRepository = departmentsRepository;
}
...
}
我还尝试遵循 本教程在 MVC 应用程序中使用 ELMAH。这个想法是使用自定义控制器工厂来处理来自每个控制器的错误。然后,您可以在 global.asax.cs 文件中将控制器工厂设置为自定义工厂。
唯一的问题是每个控制器中都需要一个无参数构造函数,而我(据我所知)无法使用 Ninject 进行依赖注入。
我该如何解决这个问题?
I'm using Ninject for dependency injection in my application. Here's an example of one of my controllers:
public class DepartmentsController : Controller
{
private IDepartmentsRepository departmentsRepository;
public DepartmentsController(IDepartmentsRepository departmentsRepository)
{
this.departmentsRepository = departmentsRepository;
}
...
}
I'm also trying to follow this tutorial to use ELMAH in an MVC application. The idea is to use a custom controller factory to handle errors from every controller. You then set the controller factory to the custom one in the global.asax.cs file.
The only problem is that it is expecting a parameterless constructor in each controller, which I can't do (that I know of) with dependency injection with Ninject.
How can I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用MVC3,您应该忽略有关控制器工厂的部分,并使用全局过滤器将自定义属性应用到每个控制器。
如果您尚未使用 v3,则可以修改其代码以从 Ninject Controller 工厂继承。
If you are using MVC3, you should ignore the part about the Controller Factory and use Global Filters to apply the custom attribute to each controller.
If you aren't using v3 yet and you can modify their code to inherit from the Ninject Controller factory.