Question

I am running a MVC 2 Preview and this is my first time trying to use Ninject2 MVC

There error that I am continently getting is: An error occurred when trying to create a controller of type 'MyMVC.Controllers.EventsController'. Make sure that the controller has a parameterless public constructor.

What I have in my Global.cs is this:

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("elmah.axd");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.MapRoute(
            "Root",
            "",
            new { controller = "Home", action = "Index", id = "" }
        );
    }

    protected override void OnApplicationStarted()
    {
        RegisterRoutes(RouteTable.Routes);
        RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    }

    protected override IKernel CreateKernel()
    {

        return new StandardKernel(new ServiceModule());
    }
}

internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IEventService>().To<EventService>();

        Bind<IEventRepository>().To<EventRepository>();

    }
}

And this is what my Controller looks like.

public class EventsController : Controller
{
    private IEventService _eventService;
    //
    // GET: /Events/

    public EventsController(IEventService eventService)
    {
        _eventService = eventService;
    }
    public ActionResult Index(string name)
    {

        return View(_eventService.GetEvent(name));
    }

    public ActionResult UpcomingEvents()
    {
        return View(_eventService.GetUpcomingEvents().Take(3).ToList());
    }

}
Was it helpful?

Solution

I've not used Ninject, but I would assume you need to implement your own IControllerFactory. Until they update it to MVC 2. Then utilize that instead of RegisterAllControllersIn(..):

ControllerBuilder.Current.SetControllerFactory(new MyNinjectControllerFactory());

EDIT: Again, i'm not all that familiar with Ninject but this might work as a simple factory:

public class MyNinjectControllerFactory : DefaultControllerFactory
{
            protected override IController GetControllerInstance(Type controllerType)
            {
               return [Container].GetInstance(controllerType) as Controller;               
            }
}

OTHER TIPS

At the risk of stating the obvious, you should try adding a parameterless constructor to your Events Controller.

public class EventsController : Controller
{
    private IEventService _eventService;
    //
    // Parameterless constructor, so NInject will work
    public EventsController() {}
    //
    // Regular constructor
    public EventsController(IEventService eventService)
    {
        _eventService = eventService;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top