gpt4 book ai didi

asp.net-mvc - MVC 基础 Controller 和 Ninject

转载 作者:行者123 更新时间:2023-12-02 01:43:44 28 4
gpt4 key购买 nike

我正在现有的 MVC 2 应用程序中实现 Ninject 依赖项注入(inject),该应用程序使用所有 Controller 继承的基本 Controller 来设置母版页所需的导航和其他信息。当我将 Controller 设置为从基本 Controller 继承时,出现以下错误:“...BaseController' 不包含采用 0 个参数的构造函数。如何解决此错误?我是 Ninject 新手,可以”弄清楚这一点。

public class BaseController : Controller
{
private INavigationRepository navigationRepository;
private ISessionService sessionService;


public BaseController(INavigationRepository navigationRepository, IMembershipService membershipService, ISessionService sessionService)
{
this.navigationRepository = navigationRepository;
this.sessionService = sessionService;
}
}


public class HomeController: BaseController
{ ... }

最佳答案

添加该 ctor 是一种方法

public class HomeController: BaseController
{
public HomeController(INavigationRepository navigationRepository, IMembershipService membershipService, ISessionService sessionService)
: base(navigationRepository, membershipService, sessionService) { }

}

或属性注入(inject)

public class BaseController : Controller
{
[Inject]
public INavigationRepository navigationRepository { get; set; }
[Inject]
public ISessionService sessionService { get; set; }


}

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

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