Getting "No controller returned error" from Ninject only in certain environments with MVC 3 application
I am currently creating an MVC3 application using Ninject. I have two controllers which get injected with a repository by Ninject, both of which work fine on my local machine.
However, when I deploy them to my remote web server, I get The IControllerFactory 'Inventory.Infrastructure.NinjectControllerFactory' did not return a controller for the name 'GetOptions'.
as an error, but only when I hit one of the controllers - the other works fine. Is there something that I'm doing wrong here? I'm pretty new to Ninject and DI so if there is another way I should be going about this, please let me know. Thanks!
Here is my controller factory
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
prot开发者_开发知识库ected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IVehicleRepository>().To<EFVehicleRepository>();
}
}
In Application_Start I have:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
And last, my controllers are
public class InventoryAPIController : Controller
{
IVehicleRepository repository;
public InventoryAPIController(IVehicleRepository repo)
{
repository = repo;
}
}
and
public class VehicleController : Controller
{
IVehicleRepository repository;
public VehicleController(IVehicleRepository repo)
{
repository = repo;
}
}
Try to use Ninject.MVC3 found on nuget instead of your custom controller factory. It uses DependencyResolver instead of a ControllerFactory, which is the prefered way for MVC3.
精彩评论