Ninject error: he IControllerFactory 'Ninject.

2019-07-18 10:52发布

I'm Getting the following yellow screen of death "The IControllerFactory 'Ninject.Web.Mvc.NinjectControllerFactory' did not return a controller for the name 'Products'." Why? Here's my setup

Update This configuration works on my computer but not on my co-worker's computer even though our computers have the same code and configuration.

Code

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

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

        }

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

        protected override IKernel CreateKernel()
        {
            var modules = new INinjectModule[]
                        {
                            new WebModule()
                        };

            return new StandardKernel(modules);
        }
    }

public class WebModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IAuthoringRepository>()
                .ToProvider(new AuthoringProvider())
                .InSingletonScope();

            Bind<ICatalogEntity>()
                .ToProvider(new ProductProvider())
                .InSingletonScope();

            Bind<TargetData>()
                .ToProvider(new TargetDataProvider()).InSingletonScope();
        }
    }

    public class AuthoringProvider : Provider<IAuthoringRepository> {
        protected override IAuthoringRepository CreateInstance(IContext context)
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            IAuthoringRepository result = RepositoryFactory.CreateAuthoringRepository(identity);
            return result;
        }
    }

    public class TargetDataProvider : Provider<TargetData>
    {
        protected override TargetData CreateInstance(IContext context)
        {
            return new TargetData { Language = Language.En, Province = Province.ON };
        }
    }

    public class ProductProvider : Provider<Product>
    {
        protected override Product CreateInstance(IContext context)
        {
            return new Product();
        }
    }

More on the error The IControllerFactory 'Ninject.Web.Mvc.NinjectControllerFactory' did not return a controller for the name 'Products'. 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 'Ninject.Web.Mvc.NinjectControllerFactory' did not return a controller for the name 'Products'.

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 'Ninject.Web.Mvc.NinjectControllerFactory' did not return a controller for the name 'Products'.]
   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() +8837208
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

2条回答
Rolldiameter
2楼-- · 2019-07-18 11:31

Update to the latest Ninject.Web.Mvc and remove this line:

RegisterAllControllersIn(Assembly.GetExecutingAssembly());

The registration is no longer needed.

查看更多
戒情不戒烟
3楼-- · 2019-07-18 11:39

try this:

routes.MapRoute(null, "",               
    new { controller = "Products", action = "Index", }
);

and make sure you have the right action called

查看更多
登录 后发表回答