Asp.net mvc 4 Ninject.Web.Common未为此对象定义无参数构造函数
我使用Nuget安装Ninject.Web.Common引用。我将它与ASP.net Web API(APIController)一起使用时没有问题,但在使用ASP.net MVC 4(控制器)时遇到了问题 错误:Asp.net mvc 4 Ninject.Web.Common未为此对象定义无参数构造函数,asp.net-mvc-4,ninject,ninject.web.mvc,ninject.web,Asp.net Mvc 4,Ninject,Ninject.web.mvc,Ninject.web,我使用Nuget安装Ninject.Web.Common引用。我将它与ASP.net Web API(APIController)一起使用时没有问题,但在使用ASP.net MVC 4(控制器)时遇到了问题 错误: No parameterless constructor defined for this object. NinjectWebCommon.cs: [assembly: WebActivator.PreApplicationStartMethod(typeof(CarsApp
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>();
}
}
}
[程序集:WebActivator.PreApplicationStartMethod(typeof(CarsApp.Web.App_Start.NinjectWebCommon),“Start”)]
[程序集:WebActivator.ApplicationShutdownMethodAttribute(typeof(CarsApp.Web.App_Start.NinjectWebCommon),“停止”)]
名称空间CarsApp.Web.App\u开始
{
使用制度;
使用System.Web;
使用Microsoft.Web.Infrastructure.DynamicModuleHelper;
使用Ninject;
使用Ninject.Web.Common;
//使用App.WebUI.Infrastructure;
使用App.Domain.Abstract;
使用App.Domain.Concreate;
使用System.Web.Http;
使用App.Web.Infrastructure;
公共静态类NinjectWebCommon
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
///
///启动应用程序
///
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestHttpModule));
RegisterModule(typeof(NinjectHttpModule));
初始化(CreateKernel);
}
///
///停止应用程序。
///
公共静态无效停止()
{
bootstrapper.ShutDown();
}
///
///创建将管理应用程序的内核。
///
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
//将基于Ninject的IDependencyResolver安装到Web API配置中
GlobalConfiguration.Configuration.DependencyResolver=新的NinjectDependencyResolver(内核);
注册服务(内核);
返回内核;
}
///
///在这里加载您的模块或注册您的服务!
///
///内核。
私有静态无效注册服务(IKernel内核)
{
kernel.Bind().To();
kernel.Bind().To();
}
}
}
尝试安装程序包Ninject.MVC3检查您的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>
这就解决了我的问题。我正在使用MVC5,并使用安装包Ninject.MVC5解决了我的问题。在屏幕右侧,您将看到至少7个带有相同错误的问题。您能告诉我们您尝试了哪些建议以及结果是什么吗?
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
</dependentAssembly>