The IControllerFactory 'SocialAssignment.WebUI.Repositories.NinjectControllerFactory' did not return a controller for the name 'AdminController'.

1,324 views
Skip to first unread message

Bo Gunnarson

unread,
Oct 20, 2010, 2:07:08 PM10/20/10
to ninject
I have a problem, that ninject not return a controller, what can i do
about this fault?

Remo Gloor

unread,
Oct 20, 2010, 2:14:28 PM10/20/10
to ninject
I think you have to provide more information. I doubt someone can give
you an answer with that little information.

Bo Gunnarson

unread,
Oct 20, 2010, 2:31:39 PM10/20/10
to ninject
What info should i pass on? im new at this...

Bo Gunnarson

unread,
Oct 20, 2010, 3:38:51 PM10/20/10
to ninject
I can show you my Ninject controller factory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using System.Web.Routing;
using Ninject.Modules;
using System.Configuration;

namespace SocialAssignment.WebUI.Repositories
{
public class NinjectControllerFactory : DefaultControllerFactory
{
// A Ninject "kernel" is the thing that can supply object
instances
private IKernel kernel = new StandardKernel(new
MyAccountServices());
// ASP.NET MVC calls this to get the controller for each
request
protected override IController
GetControllerInstance(RequestContext context,
Type controllerType)
{
if (controllerType == null)
return null;
return (IController)kernel.Get(controllerType);
}
// Configures how abstract service types are mapped to
concrete implementations
private class MyAccountServices : NinjectModule
{
public override void Load()
{
Bind<IAccountRepository>()
.To<SqlAccountRepository>()
.WithConstructorArgument("connectionString",

ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString
);
}
}
}
}


And the controller it cant instantiate...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SocialAssignment.WebUI.Repositories;

namespace SocialAssignment.WebUI.Controllers
{
public class AdminController : Controller
{
private IAccountRepository accountList;

public AdminController(IAccountRepository somereporsitory)

{
accountList = somereporsitory;
}
//
// GET: /Admin/

public ActionResult Index()
{
return View();
}


public ActionResult showAccountsList()
{
var showAccountsList = accountList.Accounts;
return View("AccountsList");

Remo Gloor

unread,
Oct 20, 2010, 5:08:31 PM10/20/10
to ninject
Hi

Can you tell us the error message?
And have you considered to use Ninject.Web.Mvc instead of implementing
you own ControllerFactory?

Remo

Bo Gunnarson

unread,
Oct 20, 2010, 5:22:24 PM10/20/10
to ninject
Well, i haven't considered that, because it just have followed a
tutorial from a MVC 2 book, and there they use that.
But i also reference to the ninject.dll

Do you think if i use the Ninject.Web.Mvc i solving this problem?

The errormessage is:

The IControllerFactory
'SocialAssignment.WebUI.Repositories.NinjectControllerFactory' did not
return a controller for the name 'AdminController'.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The
IControllerFactory
'SocialAssignment.WebUI.Repositories.NinjectControllerFactory' did not
return a controller for the name 'AdminController'.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: The IControllerFactory
'SocialAssignment.WebUI.Repositories.NinjectControllerFactory' did not
return a controller for the name 'AdminController'.]
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase
httpContext, IController& controller, IControllerFactory& factory)
+365
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase
httpContext, AsyncCallback callback, Object state) +160
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext
httpContext, AsyncCallback callback, Object state) +80

System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext
context, AsyncCallback cb, Object extraData) +45

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+8841400
System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously) +184

Remo Gloor

unread,
Oct 20, 2010, 6:21:18 PM10/20/10
to ninject
Hi

At least you would get better error messages with the Ninject
extension in case the routing is wrong.

Can you debug and get the internal exception that occurs in the
controller factory? Or is it simply that the given type is null. In
that case it is not a Ninject problem but your routing is wrong.

Remo
Reply all
Reply to author
Forward
0 new messages