gpt4 book ai didi

asp.net-mvc - Ninject 与基本 Controller ?

转载 作者:行者123 更新时间:2023-12-04 16:49:08 26 4
gpt4 key购买 nike

我想知道当你有一个基本 Controller 时,你如何使用 ninject 2.0 进行构造函数注入(inject)?

我有

        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' does not contain a constructor that takes 0 arguments

最佳答案

您需要将这两项服务注入(inject)您的 OtherController并调用传递它所需服务的基本构造函数:

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

关于asp.net-mvc - Ninject 与基本 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5720806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com