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 8471775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:53:16+00:00 2026-06-10T16:53:16+00:00

Possible Duplicate: MVC3 + Ninject – How to? In a small mvc 4 project,

  • 0

Possible Duplicate:
MVC3 + Ninject – How to?

In a small mvc 4 project, I’m trying to implement dependency injection using ninject.

So far, I have it working with api controllers, but I’m not having any luck with regular controllers.

I have A NinjectResolver:

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
    {
        private readonly IKernel _kernel;

        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            _kernel = kernel;
        }

        public IDependencyScope BeginScope()
        {
            return new NinjectDependencyScope(_kernel.BeginBlock());
        }

        public override void Dispose()
        {
            _kernel.Dispose();
        }
    }

And a NinjectScope:

public class NinjectDependencyScope : IDependencyScope
    {
        protected IResolutionRoot ResolutionRoot;

        public NinjectDependencyScope(IResolutionRoot kernel)
        {
            ResolutionRoot = kernel;
        }

        public object GetService(Type serviceType)
        {
            var request = ResolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return ResolutionRoot.Resolve(request).SingleOrDefault();
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            var request = ResolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return ResolutionRoot.Resolve(request).ToList();
        }

        public void Dispose()
        {
            var disposable = (IDisposable)ResolutionRoot;
            if (disposable != null) disposable.Dispose();
            ResolutionRoot = null;
        }
    }

Then I set it up in my Global.asax:

var kernel = new StandardKernel();
            kernel.Bind(typeof(IRepository)).To(typeof(Repository));

            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

Then I inject a repository into an api controller like this:

private IRepository _repository;

public TestApiController(IRepository repository)
{
    _repository = repository;
}

This works fine, but doing the same in a regular controller fails with the
“No parameterless constructor defined for this object” error.

Any ideas?

  • 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-10T16:53:18+00:00Added an answer on June 10, 2026 at 4:53 pm

    ASP.NET MVC uses a different IDependencyResolver than ASP.NET WebAPi namelly Sytem.Web.Mvc.IDependencyResolver.

    So you need a new NinjectDependencyResolver implementation for the MVC Controllers (luckily the MVC and WepAPi IDependencyResolver almost has the same members with the same signatures, so it’s easy to implement)

    public class NinjectMvcDependencyResolver : NinjectDependencyScope, 
                                                System.Web.Mvc.IDependencyResolver
    {
        private readonly IKernel _kernel;
    
        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            _kernel = kernel;
        }
    }
    

    And then you can register it in the Global.asax with:

    DependencyResolver.SetResolver(new NinjectMvcDependencyResolver(kernel));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Schedule Controls for ASP.Net MVC I have MVC3 in which I want
Possible Duplicate: MVC 4 Beta side by side installation error I have an MVC3
Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a
Possible Duplicate: ASP.NET MVC3 how to excute action method of controller using timer with
Possible Duplicate: Session variable is lost on RedirectToAction in IE I'm using MVC3 and
Possible Duplicate: How to do SQL Like % in Linq? Im using mvc 3
Possible Duplicate: Can I change the FormsAuthentication cookie name? I have multiple MVC3 sites
Possible Duplicate: Rendering views without master page in MVC3 I have one view in
Possible Duplicate: ASP.NET MVC on IIS 7.5 I am trying to deploy a basic
Possible Duplicate: ASP.NET MVC - View with multiple models Using ASP.NET MVC 3.0, how

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.