我在现有的MVC 2应用程序中实现Ninject依赖注入,该应用程序使用所有控制器继承的基本控制器来设置母版页所需的导航和其他信息。当我将控制器设置为从基本控制器继承时,我收到以下错误:“... 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
{ ... }
答案 0 :(得分:6)
添加ctor是一种方式
public class HomeController: BaseController
{
public HomeController(INavigationRepository navigationRepository, IMembershipService membershipService, ISessionService sessionService)
: base(navigationRepository, membershipService, sessionService) { }
}
或财产注入
public class BaseController : Controller
{
[Inject]
public INavigationRepository navigationRepository { get; set; }
[Inject]
public ISessionService sessionService { get; set; }
}