Ninject有一个基本控制器?

时间:2011-04-19 18:18:14

标签: asp.net-mvc asp.net-mvc-3 ninject

我想知道如果你有一个基本控制器,你如何使用ninject 2.0进行构造函数注入?

我有

        private readonly IBaseService baseService;

        public BaseController(IBaseService baseService)
        {
            this.baseService = baseService;

        }


Bind<IBaseService>().To<BaseService>();


public class OtherController : BaseController
{
        private readonly IOtherService otherService;

        public OtherController(IOtherService otherService, IBaseService baseService) 
        {
            this.otherService = otherService;
        }

但我得到了

  

'BaseController'不包含   取0参数的构造函数

2 个答案:

答案 0 :(得分:13)

您需要将两个服务注入OtherController并调用基础构造函数来传递它所需的服务:

public OtherController(IOtherService otherService, IBaseService baseService)
    : base(baseService) { this.otherService = otherService; }

答案 1 :(得分:1)

你必须链接到基本控制器,没有?

public OtherController(IOtherService otherService, IBaseService baseService) : base(baseService)