我有一个实现Ninject的Web API 2项目。如果我的控制器不使用Route属性,它工作正常但是,如果我使用它们,应用程序将返回以下异常:“尝试创建'AccountsController'类型的控制器时发生错误。确保控制器具有无参数公共构造函数“。
工作正常
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
不要工作
[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
我安装了以下软件包
Ninject version="3.2.0.0"
Ninject.Extensions.ContextPreservation version="3.2.0.0"
Ninject.Extensions.NamedScope version="3.2.0.0"
Ninject.Web.Common version="3.2.0.0"
Ninject.Web.Common.OwinHost version="3.2.3.0"
Ninject.Web.Common.WebHost version="3.2.3.0"
Ninject.Web.WebApi version="3.2.4.0"
Ninject.Web.WebApi.OwinHost version="3.2.4.0"
Ninject.Web.WebApi.WebHost
我的NintextWebCommon类是
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Stop")]
namespace ProperdiAPI.App_Start
{
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using System;
using System.Web;
using System.Web.Http;
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);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IEntityService>().To<EntityService>().InRequestScope();
}
}
}
BaseApiController
public class BaseApiController : ApiController
{
private IEntityService entitysService;
public BaseApiController(IEntityService entityService)
{
this.entityService = entityService;
}
protected IEntityService EntitysService
{
get
{
return this.entityService;
}
}
protected IHttpActionResult GetErrorResult(IdentityResult result)
{
if (result == null)
{
return InternalServerError();
}
if (!result.Succeeded)
{
if (result.Errors != null)
{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
if (ModelState.IsValid)
{
return BadRequest();
}
return BadRequest(ModelState);
}
return null;
}
}
}
控制器
[RoutePrefix("api/accounts")]
public class AccountsController : BaseApiController
{
public AccountsController(IEntityService entityService)
:base(entityService)
{
}
[HttpGet]
//[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService .getAll();
return Ok(entities);
}
}
我尝试了很多东西,例如构建自定义解析器和范围,安装旧的ninject版本等等,但没有任何作用。
提前多多感谢!
答案 0 :(得分:0)
安装插件Ninject.WebApi.DependencyResolver
并在调用RegisterServices(kernel);
后添加此代码:
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);