ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi [英] ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi

查看:27
本文介绍了ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi.在同一个项目中,简单的控制器与 Unity.Mvc3 一起正常工作.但是当我运行从 ApiController 派生的 Web API 控制器时,我收到一条消息:

My ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi. In the same project simple controllers works with Unity.Mvc3 properly. But when I run Web API controller derived from ApiController I'm getting a message:

{"$id":"1","Message":"发生错误.","ExceptionMessage":"Type'ElectricTests.Controllers.Api.DocumentsController' 没有默认构造函数","ExceptionType":"System.ArgumentException","StackTrace":"在 System.Linq.Expressions.Expression.New(Type type)\r\n 在System.Web.Http.Internal.TypeActivator.Create[TBase](Type实例类型)\r\n 在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage请求,类型控制器类型,Func`1&激活器)\r\n 在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor 控制器描述符,类型控制器类型)"}

{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Type 'ElectricTests.Controllers.Api.DocumentsController' does not have a default constructor","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 at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}

我的 ApiController:

My ApiController:

public class DocumentsController : ApiController
{
    private readonly IDocumentsRepository _repository;

    public DocumentsController(IDocumentsRepository repository) {
        _repository = repository;
    }

    public IEnumerable<FormattedDocument> GetFormattedDocuments()
    {
        return _repository.GetAllFormattedDocuments();
    }
    ...

Bootstrapper.cs:

Bootstrapper.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;
    }
}

我的错误在哪里?

推荐答案

Controller 和 ApiController 的处理方式不同,因为它们的基类完全不同:

The handling of Controller and ApiController is different as they have completely different base classes:

我将 Unity.MVC4 库用于控制器 DI (http://www.nuget.org/packages/Unity.MVC4/)

I use Unity.MVC4 library for controller DI (http://www.nuget.org/packages/Unity.MVC4/)

Install-Package Unity.MVC4

和用于 DI 的 Unity.WebAPI (http://www.nuget.org/packages/Unity.WebAPI/)

and Unity.WebAPI for DI (http://www.nuget.org/packages/Unity.WebAPI/)

Install-Package Unity.WebAPI

你的引导程序应该是两者的结合:

Your bootstrapper should be a combination of both:

DependencyResolver.SetResolver(new Unity.Mvc4.UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

注意我还必须添加一些注册才能使帮助页面正常工作

Note I also had to do to add some registration to get the Help page to work

container.RegisterInstance(typeof (HttpConfiguration), GlobalConfiguration.Configuration);

作为 Unity.MVC4 的所有者,我正在考虑在我们的库中实现 WebApi.

As the owner of Unity.MVC4 I am looking at getting WebApi implemented within our library.

这篇关于ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆