Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:26:36+00:00 2026-06-13T07:26:36+00:00

I’m having a trouble, making Ninject and WebAPI.All working together. I’ll be more specific:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:26:37+00:00Added an answer on June 13, 2026 at 7:26 am

    Here are a couple of steps I have compiled for you that should get you started:

    1. Create a new ASP.NET MVC 3 project using the Internet Template
    2. Install the following 2 NuGets: Microsoft.AspNet.WebApi and Ninject.MVC3
    3. Define an interface:

      public interface IRepository
      {
          string GetData();
      }
      
    4. And an implementation:

      public class InMemoryRepository : IRepository
      {
          public string GetData()
          {
              return "this is the data";
          }
      }
      
    5. Add an API controller:

      public class ValuesController : ApiController
      {
          private readonly IRepository _repo;
          public ValuesController(IRepository repo)
          {
              _repo = repo;
          }
      
          public string Get()
          {
              return _repo.GetData();
          }
      }
      
    6. Register an API Route in your Application_Start:

      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      
          GlobalConfiguration.Configuration.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
      
          routes.MapRoute(
              "Default",
              "{controller}/{action}/{id}",
              new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
      }
      
    7. Add a custom Web API Dependency Resolver using Ninject:

      public class LocalNinjectDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver
      {
          private readonly IKernel _kernel;
      
          public LocalNinjectDependencyResolver(IKernel kernel)
          {
              _kernel = kernel;
          }
      
          public System.Web.Http.Dependencies.IDependencyScope BeginScope()
          {
              return this;
          }
      
          public object GetService(Type serviceType)
          {
              return _kernel.TryGet(serviceType);
          }
      
          public IEnumerable<object> GetServices(Type serviceType)
          {
              try
              {
                  return _kernel.GetAll(serviceType);
              }
              catch (Exception)
              {
                  return new List<object>();
              }
          }
      
          public void Dispose()
          {
          }
      }
      
    8. Register the custom dependency resolver inside the Create method (~/App_Start/NinjectWebCommon.cs):

      /// <summary>
      /// Creates the kernel that will manage your application.
      /// </summary>
      /// <returns>The created kernel.</returns>
      private static IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
          kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
      
          RegisterServices(kernel);
      
          GlobalConfiguration.Configuration.DependencyResolver = new LocalNinjectDependencyResolver(kernel); 
          return kernel;
      }
      
    9. Configure the kernel inside the RegisterServices method (~/App_Start/NinjectWebCommon.cs):

      /// <summary>
      /// Load your modules or register your services here!
      /// </summary>
      /// <param name="kernel">The kernel.</param>
      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind<IRepository>().To<InMemoryRepository>();
      }        
      
    10. Run the application and navigate to /api/values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.