I'm having a trouble, making Ninject and WebAPI.All working together. I'll be more specific:
First, I played around with WebApi.All package and looks like it works fine to me.
Second, I added to RegisterRoutes
in Global.asax
next line:
routes.Add(new ServiceRoute("api/contacts", new HttpServiceHostFactory(), typeof(ContactsApi)));
So the final result was:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("api/contacts", new HttpServiceHostFactory(), typeof(ContactsApi)));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
Everything seems to be fine, but when I'm trying to redirect user to a specific action, something like:
return RedirectToAction("Index", "Home");
Url in browser is localhost:789/api/contacts?action=Index&controller=Home
Which is not good. I swaped lines in RegisterRoute
and now it looks:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.Add(new ServiceRoute("api/contacts", new HttpServiceHostFactory(), typeof(ContactsApi)));
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
Now the redirection works fine, but when I try to access to my API actions, I get error telling me that
Ninject couldn't return controller "api"
which is absolutely logical, I don't have such controller.
I did search some info how to make Ninject work with WebApi, but everything I found was only for MVC4 or .Net 4.5. By the technical issues I can't move my project to a new platform, so I need to find a working solution for this versions.
This answer looked like a working solution, but when I'm trying to launch project I get compiler error in line
CreateInstance = (serviceType, context, request) => kernel.Get(serviceType);
telling me:
System.Net.Http.HttpRequestMessage is defined in an assembly that is not referenced
and something about adding refference in assembly
System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
I have no Idea what to do next, I couldn't find any useful info about ninject with webapi on .NET 4 and MVC3. Any help would be appreciated.