views:

58

answers:

2

I have a new MVC 2 project using ninject 2 for IOC. I have the following global.asax which sets up both NHibernate and Ninject. The code runs fine, ninject pulls out the controllers from the assembly (it does convert them to lowe case strings when it does this - inside the Ninject source).

All my controller URL's are now case sensitive so, /Home won't resolve but /home will.

When I use an uppercase first letter (the defualt in MVC) I get the error "The IControllerFactory 'Ninject.Web.Mvc.NinjectControllerFactory' did not return a controller for the name 'Home'."

Surly this isn't normal? Any Ideas?

public class MvcApplication : NinjectHttpApplication { public static ISessionFactory SessionFactory = CreateSessionFactory(); public MvcApplication() { this.BeginRequest += new EventHandler(MvcApplication_BeginRequest); this.EndRequest += new EventHandler(MvcApplication_EndRequest); }

    void MvcApplication_BeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(SessionFactory.OpenSession());
    }

    void MvcApplication_EndRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Unbind(SessionFactory).Dispose();
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    }

    private static ISessionFactory CreateSessionFactory()
    {
        var cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
        cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Data Source=.\;Initial Catalog=xxxxxx;Integrated Security=true;");
        //cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, System.Environment.MachineName);
        NHibernateProfiler.Initialize();
        return cfg.BuildSessionFactory();
    }

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

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] { 
        new ServiceModule(),
    });

    }
}
A: 

I'm wondering what the RegisterAllControllersIn(Assembly.GetExecutingAssembly()); line does? I'm not seeing that in the Ninject code. You shouldn't need to call any specific controller registrationg, since Ninject will find them and construct them on its own.

dave thieben
When I run the program in debug and follow it along it does find all the contollers (currently just the Home and Accounts controllers, as its a fresh project). It's just it seems to register them as 'home' and 'accounts' all lower case, so when I try and browse to them it fails unless I adjust the case.
Orange
I'm sure you know this but to be clearer the RegisterAllControllersIn(Assembly.GetExecutingAssembly()); calls a method of the subclass NinjectHttpApplication, it's what makes Ninject crawl through and pull out the controllers. NinjectHttpApplication is from Ninject.Web.Mvc which is a dll to make Ninject integrate nicely with MVC. I just can't figure out why it's case sensitive.
Orange
Aha... I was looking at the code for NinjectHttpApplication in the MVC2 folder of the Ninject code, it appears RegisterAllControllers is only in the MVC1 folder. I'd try using the MVC2 code/binaries and see where that gets you.
dave thieben
A: 

RegisterAllControllersIn isn't used anymore in the latest version of the extension combined with Ninject 2.1. Therefore, I succest to uptate to the latest version of Ninject and the MVC extension. http://teamcity.codebetter.com/project.html?projectId=project3&tab=projectOverview

There is also a sample project on GitHub based on the VS2010 Sample project: http://github.com/ninject/ninject.web.mvc/tree/master/mvc2/src/SampleApplication/

Remo Gloor