开发者

Binding Ninject to child Controller - Error: did not return controller for

开发者 https://www.devze.com 2023-03-05 13:20 出处:网络
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\'.

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<Soccer开发者_高级运维Team>>().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! .


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);


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号