您的控制器类有问题,具有依赖项的构造函数是private。你的控制器应该是这样的:
public class HomeController : Controller
{
private readonly IMilestoneService _service;
public HomeController(IMilestoneService service)
{
_service = service;
}
}
甚至不包含公共无参数构造函数,它甚至无效,你的类needs对功能的依赖。
事实上,我还在构造函数中插入了针对该依赖项的空检查,以确保我的类在构造时有效:
public class HomeController : Controller
{
private readonly IMilestoneService _service;
public HomeController(IMilestoneService service)
{
_service = service;
Enforce.NotNull(() => _service); // lambda to auto-magically get variable name for exception
}
}
您的 MvcApplication 类也可能存在问题。
代替protected void Application_Start()
,有一个不同的函数可以覆盖,protected override void OnApplicationStarted()
这是您对设置路由的调用应该进行的地方:
public class MvcApplication : NinjectHttpApplication
{
public override void Init()
{
base.Init();
Mappers.Initialize();
}
protected override Ninject.IKernel CreateKernel()
{
return Ioc.Initialize();
}
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
Routing.RegisterRoutes(routes);
//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
}
当然,如果您已经打电话Application_Start
也不错,不过我在OP里没看到。