xiecs
xiecs

Reputation: 727

The IControllerFactory 'MyWebSite.WebUI.Infrastructure.NinjectControllerFactory' did not return a controller for the name 'Admin'

I am getting the above when I try and open a view in a controller in an Area. Ninject is set up as follows:

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel kernel = new StandardKernel(new RLSBCWebSiteServices());

    protected override IController GetControllerInstance(RequestContext context, Type controllerType)
    {
        if (controllerType == null)
            return null;
        return (IController)kernel.Get(controllerType);
    }

    private class MyWebSiteServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IMatchesRepository>().To<SqlMatchesRepository>().WithConstructorArgument("connectionString",
                ConfigurationManager.ConnectionStrings["MyWebSiteDb"].ConnectionString);
        }
    }
}

If I place a breakpoint in the code, I see the RequestContext context contains the following values:

context.RouteData.DataTokens.Values[0] = “MyWebSite.WebUI.Areas.Visitor”  context.RouteData.DataTokens.Values[1] = “Visitor” which is the Area  
context.RouteData.Values.Values[0] = “admin” which is the Controller  
context.RouteData.Values.Values[1] = “register” which is the View

However controllerType == null, instead on the controller name.

This transfer to the new page is being triggered by

Html.ActionLink("here", "Register", "Admin", new { area = "Visitor" }, null)

which is on the Login page. However the same thing happens if I enter

http://example.com/Visitor/admin/register

into IE8

The area registration is as follows:

public class VisitorAreaRegistration : AreaRegistration
{
    public override string AreaName { get { return "Visitor"; } }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Visitor_default",
            "Visitor/{controller}/{action}/{id}",
            new { controller = "Admin", action = "Register", id = UrlParameter.Optional }
        );
    }
}

Has anyone managed to get Areas working with NinjectControllerFactory, or is there something wrong with my set-up?

Upvotes: 5

Views: 5334

Answers (2)

Nadir Azizov
Nadir Azizov

Reputation: 11

Check your controller and action name in view. I also got same error as an action name was wrong.

Upvotes: 1

Remo Gloor
Remo Gloor

Reputation: 32725

Instead of creating your own NinjectControllerFactory use the latest version of Ninject.Web.Mvc. It supports Areas. See: https://github.com/ninject/ninject.web.mvc

Upvotes: 3

Related Questions