Using a parameterless controller constructor with Ninject?

孤人 提交于 2019-12-13 18:13:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4839801/using-a-parameterless-controller-constructor-with-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!