0
votes

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(),
    });

    }
}
2

2 Answers

0
votes

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.

0
votes

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/