tags:

views:

80

answers:

1

How can you instantiate a Controller that has an int argument? Using Ninject..

My HomeController has a constructor like this:


  private int _masterId;
  Public HomeController(int masterId){
       _masterId = masterId;
}

I created a controller factory like this:


public class NinjectControllerFactory : DefaultControllerFactory
    {
        IKernel kernel = new StandardKernel(new ExampleConfigModule());

        protected override IController GetControllerInstance(Type controllerType)
        {

            return controllerType == null ? null
                                          : (IController)kernel.Get(controllerType, 1);

        }

    }

+1  A: 

I'm not sure if this would work with Ninject 1.0, but does work with 2.0

var controller = kenel.Get<IController>(new ConstructorArgument("masterId", 1)); 

However it's probably not too good idea to pass arguments to constructor manually when using IoC container.

miensol