在Ninject上使用无参数控制器构造函数? [英] Using a parameterless controller constructor with Ninject?

查看:144
本文介绍了在Ninject上使用无参数控制器构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用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;
    }

    ...
}

我也在尝试遵循本教程在MVC应用程序中使用ELMAH.想法是使用自定义控制器工厂来处理每个控制器的错误.然后,将控制器工厂设置为global.asax.cs文件中的自定义工厂.

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.

唯一的问题是,每个控制器都希望有一个无参数的构造函数,而我使用Ninject进行依赖注入无法做到这一点(我知道).

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.

我该如何解决?

推荐答案

如果使用的是MVC3,则应忽略有关Controller Factory的部分,并使用Global Filters将自定义属性应用于每个控制器.

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.

如果您尚未使用v3,则可以修改其代码以从Ninject Controller工厂继承.

If you aren't using v3 yet and you can modify their code to inherit from the Ninject Controller factory.

这篇关于在Ninject上使用无参数控制器构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆