我的 ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi。在同一个项目中,简单的控制器可以正常使用 Unity.Mvc3。但是当我运行从 ApiController 派生的 Web API 控制器时,我收到一条消息:
{"$id":"1","Message":"发生错误。","ExceptionMessage":"类型 'ElectricTests.Controllers.Api.DocumentsController' 没有默认构造函数","ExceptionType":" System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\ n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型控制器类型,Func`1& 激活器)\r\n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor 控制器描述符,类型控制器类型)"}
我的 API 控制器:
public class DocumentsController : ApiController
{
private readonly IDocumentsRepository _repository;
public DocumentsController(IDocumentsRepository repository) {
_repository = repository;
}
public IEnumerable<FormattedDocument> GetFormattedDocuments()
{
return _repository.GetAllFormattedDocuments();
}
...
引导程序.cs:
public static class Bootstrapper {
public static void Initialise() {
IUnityContainer container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer() {
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IDocumentsRepository, DocumentsRepository>();
container.RegisterType<IQuestionsRepository, QuestionsRepository>();
container.RegisterType<ITestRepository, TestsRepository>();
return container;
}
}
我的错误在哪里?