2

我使用 Nuget 来安装 Ninject.Web.Common 参考。我将它与 ASP.net Web API (APIController) 一起使用没有问题,但是在使用 ASP.net MVC 4(Controller) 时遇到了问题。

错误:

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>();
        }        
    }
}
4

3 回答 3

1

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

于 2015-05-27T06:24:21.580 回答
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>

这为我做到了。

于 2014-12-15T09:31:35.933 回答
0

尝试使用 Install-Package Ninject.MVC3

于 2014-07-09T13:45:28.097 回答