ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi asp.net asp.net

ASP.Net MVC 4 Web API controller doesn't work with Unity.WebApi


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

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

Install-Package Unity.MVC4

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);

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


When you install Unity for ASP.NET Web API, it does everything except add the following line to your Global.asax

Bootstrapper.Initialise();

So you need to add that to your Application_Start method:

protected void Application_Start(){    AreaRegistration.RegisterAllAreas();    Bootstrapper.Initialise();    WebApiConfig.Register(GlobalConfiguration.Configuration);    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);    RouteConfig.RegisterRoutes(RouteTable.Routes);    BundleConfig.RegisterBundles(BundleTable.Bundles);}