title: Determine which construtor to be called(change default constructor) when an action is called in MVC Web API
body: <p>I'm using mvc web api. I have a generic IRepository and IUnitOfWork library which works perfect in asp.net mvc but when I intended to use it in my mvc web api project my repository object becomes null and consequently leads to exception. my repository and UnitOfWork objects are being instantiated and initialized inside a constructor which takes one <code>IunitOfWork</code> object as a parameter </p>
  
  <p>here is the code (constructor code)</p>
  
  <pre><code>private IRepository&lt;AspNetUserAccount&gt; AccountRepository;
      private IRepository&lt;Doctor&gt; DoctorRepository;
      private readonly IUnitOfWork UnitOfWork;
      public AccountController(IUnitOfWork unitOfWork)
      {
          UnitOfWork = unitOfWork;
          AccountRepository = unitOfWork.Repository&lt;AspNetUserAccount&gt;();
          DoctorRepository = unitOfWork.Repository&lt;Doctor&gt;();
      }
      public AccountController()
      {
  
      }
  </code></pre>
  
  <p>and here is the usage of my Repository and UnitOfWork objects in <code>Register</code> action of <code>Account</code> controller.</p>
  
  <pre><code> [AllowAnonymous]
   [Route("Register")]
   public ResultViewModel Register([FromBody]UserBindingModel model)
      {
          if (!ModelState.IsValid)
          {
              return new ResultViewModel(400, "Model Is Not Valid");
          }
          using (DigiTebDBContext db = new DigiTebDBContext())
          {
              using (var Transaction = db.Database.BeginTransaction())
              {
                  try
                  {
                      var user = new ApplicationUser()
                      {
                          UserName = model.UserName,
                          Email = model.Email
                          ,
                          CreateTimeStamp = DateTime.Now,
                          LastModifiedTimeStamp = DateTime.Now
                      };
                      IdentityResult result = UserManager.Create(user, model.Password);
  
                      if (!result.Succeeded)
                          throw new Exception(((string[])result.Errors)[0]);
                      UserUtility.AssignRoleToUser(user.Id, "Operator");
                      var Account = new AspNetUserAccount
                      {
                          AccountTypeValue = AccountType.Demo,
                          CreateTimeStamp = DateTime.Now
                      ,
                          CreateUserId = user.Id,
                          CurrentUserCount = 0,
                          MaxUserCount = 1,
                          IsMultiUser = false
                      ,
                          ExpiryDate = DateTime.Now,
                          LastModifiedUserId = user.Id,
                          LastModifiedTimeStamp = DateTime.Now
                      };
                      AccountRepository.Insert(Account);
                      UnitOfWork.Save();
  
                      Transaction.Commit();
                      return new ResultViewModel(200, "Registeration was successfull");
                  }
                  catch(Exception ex)
                  {
                      Transaction.Rollback();
                      return new ResultViewModel(400, ex.Message);
                  }
                  finally
                  {
                      UnitOfWork.Dispose();
                  }
  
              }
          }
  </code></pre>
  
  <p>My problem is that my Repository <code>(AccountRepository)</code> and UnitOfWork <code>(UnitOfWork)</code> objects are <strong><em>being instantiated and initialized in a constructor that will never be called</em></strong>. <strong>when an action is called</strong> (no matter what) <strong>only parameterless constructor is being called and not my desired constructor</strong> with one parameter. </p>
  
  <p>How can I change it so that the desired constructor being called instead of default parameterless constructor, in other word <strong><em>how can I determine which constructor is being called when an action is called in mvc web api??</em></strong>?</p>
  
  <p>I searched a bit about it and I figured that I can use the DI to resolve this issue, I used Ninject but I don't know how and where exactly I should write my codes <strong>(using Ninject) to change the default constructor of my <code>Account</code> class (Controller) and set it to another constructor</strong> </p>
  
  <p>here is my Ninject modifications in <code>NinjectWebCommon.cs</code> in <code>App_Start</code> folder</p>
  
  <pre><code> private static IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          try
          {
              kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);
              kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();
  
              RegisterServices(kernel);
              return kernel;
          }
          catch
          {
              kernel.Dispose();
              throw;
          }
      }
      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind(typeof(IRepository&lt;AspNetUserAccount&gt;)).To(typeof(Repository&lt;AspNetUserAccount&gt;)).InRequestScope();
          kernel.Bind(typeof(IDataContext)).To(typeof(DigiTebDBContext)).InRequestScope();
  
          kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork)).InRequestScope();
  
          kernel.Bind&lt;HttpContext&gt;().ToMethod(ctx =&gt; HttpContext.Current).InTransientScope();
      }        
  </code></pre>
  
  <p>Thanks in advance      </p>