2013-10-18 114 views
2

我使用Nuget来安装Ninject.Web.Common引用。我用它与ASP.net Web API(APIController)没有问题,但我使用ASP.net MVC 4(控制器)时出现问题。Ninject.Web.Common没有为此对象定义的无参数构造函数

错误:

No parameterless constructor defined for this object. 

NinjectWebCommon.cs:

[assembly: WebActivator.PreApplicationStartMethod(typeof(CarsApp.Web.App_Start.NinjectWebCommon), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(CarsApp.Web.App_Start.NinjectWebCommon), "Stop")] 

namespace CarsApp.Web.App_Start 
{ 
    using System; 
    using System.Web; 

    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 

    using Ninject; 
    using Ninject.Web.Common; 
    //using App.WebUI.Infrastructure; 
    using App.Domain.Abstract; 
    using App.Domain.Concreate; 
    using System.Web.Http; 
    using App.Web.Infrastructure; 

    public static class NinjectWebCommon 
    { 
     private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

     /// <summary> 
     /// Starts the application 
     /// </summary> 
     public static void Start() 
     { 
      DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
      DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
      bootstrapper.Initialize(CreateKernel); 
     } 

     /// <summary> 
     /// Stops the application. 
     /// </summary> 
     public static void Stop() 
     { 
      bootstrapper.ShutDown(); 
     } 

     /// <summary> 
     /// Creates the kernel that will manage your application. 
     /// </summary> 
     /// <returns>The created kernel.</returns> 
     private static IKernel CreateKernel() 
     { 
      var kernel = new StandardKernel(); 
      kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
      kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

      // Install our Ninject-based IDependencyResolver into the Web API config 
      GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); 

      RegisterServices(kernel); 
      return kernel; 
     } 

     /// <summary> 
     /// Load your modules or register your services here! 
     /// </summary> 
     /// <param name="kernel">The kernel.</param> 
     private static void RegisterServices(IKernel kernel) 
     { 
      kernel.Bind<IMembershipRepository>().To<EFMembershipRepository>(); 
      kernel.Bind<IBranchRepository>().To<EFBranchRepository>(); 
     }   
    } 
} 
+1

在屏幕的右侧,您会看到至少7个具有相同错误的问题。你能告诉我们你有什么建议和结果吗? –

回答

0

与安装,包装Ninject.MVC3

0

检查尝试,如果在你的web.config你有两行MVC版本控制,我有类似的东西:

<dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
    </dependentAssembly> 
<dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="4.0.0.0-4.0.0.1" newVersion="4.0.0.1" /> 
</dependentAssembly> 

如果然后像一条线取代他们,重定向到最高版本,您使用的是在我的情况下,它4.0.0.1

<dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" /> 
    </dependentAssembly> 

这为我做。

0

我使用MVC5并使用Install-Package Ninject.MVC5解决了我的问题。

相关问题