将 Ninject 绑定到子控制器 - 错误:未返回控制器

发布于 2024-11-07 10:43:18 字数 1738 浏览 0 评论 0原文

我正在尝试将一些存储库绑定到子控制器,但我不断收到错误消息,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2024-11-14 10:43:18

你想要:

public class SoccerController : TeamController<SoccerTeam>
{
    public SoccerController(ITeamRepository<SoccerTeam> teamRepository) : base(teamRepository)
    {

    }
}

        Bind(typeof(ITeamRepository<>)).To(typeof(TeamRepository<>))
            .WithConstructorArgument("connectionString",
                ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString);

You want:

public class SoccerController : TeamController<SoccerTeam>
{
    public SoccerController(ITeamRepository<SoccerTeam> teamRepository) : base(teamRepository)
    {

    }
}

and

        Bind(typeof(ITeamRepository<>)).To(typeof(TeamRepository<>))
            .WithConstructorArgument("connectionString",
                ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString);
Saygoodbye 2024-11-14 10:43:18

您创建的控制器工厂中是否也有以下方法(或类似的方法)来替换默认控制器工厂?

protected override IController GetControllerInstance(RequestContext context, Type controllerType) {
    if (controllerType == null) return null;

    return ((IController)_kernel.Get(controllerType));
}

_kernel 是该控制器工厂的私有成员变量(实现IKernel)。

Do you also have the following method (or something similar) in the controller factory you created to replace the default controller factory?

protected override IController GetControllerInstance(RequestContext context, Type controllerType) {
    if (controllerType == null) return null;

    return ((IController)_kernel.Get(controllerType));
}

And _kernel is a private member variable of that controller factory (implements IKernel).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文