views:

81

answers:

1

I downloaded the fairly new Ninject 2.0 and Ninject.Web.Mvc (targeting mvc2) sources today, and successfully built them against .NET 4 (release configuration). When trying to run an application using Ninject 2.0, i keep getting 404 errors and I can't figure out why.

This is my global.asax.cs (slightly shortified, for brevity):

using ...
using Ninject;
using Ninject.Web.Mvc;
using Ninject.Modules;

namespace Booking.Web
{
    public class MvcApplication : NinjectHttpApplication
    {        
        protected override void OnApplicationStarted()
        {
            Booking.Models.AutoMapperBootstrapper.Initialize();
            RegisterAllControllersIn(Assembly.GetExecutingAssembly());
            base.OnApplicationStarted();
        }

        protected void RegisterRoutes(RouteCollection routes)
        {
            ...
            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Entry", action = "Index", id = "" }
            );    
        }

        protected override IKernel CreateKernel()
        {
            INinjectModule[] mods = new INinjectModule[] {...};
            return new StandardKernel(mods);
        }
    }
}

The EntryController exists, and has an Index method that simply does a return View(). I have debugged, and verified that the call to RegisterAllControllersIn() is executed. I have also tried to use Phil Haacks Routing debugger but I still get a 404.

What do I do to find the cause of this?

A: 

Are your routes being registered? Is that being called from the base class?

Also make sure that you are registering your controllers properly. I'm not sure how Ninject's controller factory expects it, but it might require a specific name or something.

Ben Scheirman
In Ninject 1.5, this was done automatically by the `NinjectHttpApplication` that I inherit. I took a look at the assembly in Reflector, and found no call to `RegisterRoutes()` in this version, but I'm not sure I can do one myself. Where do I get the `RouteTable` I need as argument?
Tomas Lycken
Brilliant! I examined the `Application_Start()` method in `NinjectHttpApplication` even closer, and noticed that there was a binding for `RouteCollection` there. Adding a simple `RegisterRoutes(Kernel.Get<RouteCollection>());` to my init method solved the problem. Thanks!
Tomas Lycken