ASP.Net MVC3:史蒂文·桑德森的教程 - 使用Ninject错误



我的项目错误与全局。asax Application_Start调用:ControllerBuilder.Current。SetControllerFactory(新NinjectControllerFactory ());

namespace SportsStore.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory 
    {
        private IKernel ninjectKernel;
        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }
        private void AddBindings()
        {
            // put additional bindingers here
        }
    }

错误堆栈:

Locating source for 'c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs'. 
Checksum: MD5 {de 1d cc 43 b7 22 44 a5 8d 8b 50 ed 23 dc 4 28} 
The file 'c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs' does not exist. 
Looking in script documents for 'c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs'... 
Looking in the projects for 'c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs'. 
The file was not found in a project. Looking in directory 'C:Program FilesMicrosoft Visual Studio 10.0VCcrtsrc'... 
Looking in directory 'C:Program FilesMicrosoft Visual Studio 10.0VCatlmfcsrcmfc'... 
Looking in directory 'C:Program FilesMicrosoft Visual Studio 10.0VCatlmfcsrcatl'...
Looking in directory 'C:Program FilesMicrosoft Visual Studio 10.0VCatlmfcinclude'... 
The debugger will ask the user to find the file: c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs. 
The user pressed Cancel in the Find Source dialog. 
The debug source files settings for the active solution have been modified so that the debugger will not ask the user to find the file: c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs. 
The debugger could not locate the source file 'c:ProjectsNinjectMaintenance2.2ninjectsrcNinjectInfrastructureMultimap.cs'.

我建议使用官方Ninject MVC3扩展。NuGet包Ninject。MVC3将为您设置一切,以便您可以开始将依赖项注入到控制器中。

替换控制器工厂不再是MVC3的首选方式。在这个版本中,MVC开发团队建议的方法是使用DependencyResolver。Ninject MVC3扩展使用这种方法。

阅读https://github.com/ninject/ninject.web.mvc/wiki/MVC3的文档获取更多信息

相关内容

最新更新