将 Ninject 绑定到子控制器 - 错误:未返回控制器
我正在尝试将一些存储库绑定到子控制器,但我不断收到错误消息,NinjectControllerFactory' 没有返回名称为“soccer”的控制器。
基本控制器:
public class TeamController<T> : Controller
{
protected readonly ITeamRepository<T> TeamRepository;
public TeamController(ITeamRepository<T> teamRepository)
{
TeamRepository = teamRepository;
}
public ViewResult Teams(string viewName, string masterName, object model)
{
return View("~/Views/Teams.aspx", TeamRepository.Team.ToList());
}
}
然后是足球控制器:
public class SoccerController<T> : TeamController<T> where T : class
{
public SoccerController(ITeamRepository<T> teamRepository) : base(teamRepository)
{
}
}
Ninject:
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel = new StandardKernel(new MyService());
protected override IController GetControllerInstance(RequestContext context, Type controllerType)
{
if (controllerType == null) return null;
return (IController) _kernel.Get(controllerType);
}
private class MyService : NinjectModule
{
public override void Load()
{
Bind<ITeamRepository<SoccerTeam>>().To<TeamRepository<SoccerTeam>>()
.WhenInjectedInto(typeof(SoccerController<SoccerTeam>))
.WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString);
}
}
}
现在,当我点击 localhost/soccer/teams 时,我收到一条错误,指出 NinjectControllerFactory 没有返回名称为“soccer”的控制器。我缺少什么?
提前致谢! 。
I'm attempting to bind some repositories to child controller, but I keep on getting en error that NinjectControllerFactory' did not return a controller for the name 'soccer'.
Base Controller:
public class TeamController<T> : Controller
{
protected readonly ITeamRepository<T> TeamRepository;
public TeamController(ITeamRepository<T> teamRepository)
{
TeamRepository = teamRepository;
}
public ViewResult Teams(string viewName, string masterName, object model)
{
return View("~/Views/Teams.aspx", TeamRepository.Team.ToList());
}
}
Then Soccer Controller:
public class SoccerController<T> : TeamController<T> where T : class
{
public SoccerController(ITeamRepository<T> teamRepository) : base(teamRepository)
{
}
}
Ninject:
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel = new StandardKernel(new MyService());
protected override IController GetControllerInstance(RequestContext context, Type controllerType)
{
if (controllerType == null) return null;
return (IController) _kernel.Get(controllerType);
}
private class MyService : NinjectModule
{
public override void Load()
{
Bind<ITeamRepository<SoccerTeam>>().To<TeamRepository<SoccerTeam>>()
.WhenInjectedInto(typeof(SoccerController<SoccerTeam>))
.WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString);
}
}
}
Now when I hit localhost/soccer/teams I get an error stating that NinjectControllerFactory did not return a controller for the name 'soccer'. What am I missing?
Thanks in advance!
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想要:
和
You want:
and
您创建的控制器工厂中是否也有以下方法(或类似的方法)来替换默认控制器工厂?
_kernel
是该控制器工厂的私有成员变量(实现IKernel
)。Do you also have the following method (or something similar) in the controller factory you created to replace the default controller factory?
And
_kernel
is a private member variable of that controller factory (implementsIKernel
).