Search code examples
c#inheritanceasp.net-mvc-5asp.net-identity

How to make Account and Manage controllers inherit from a BaseController without errors


I've made a custom controller called BaseController and I'm making all my controllers inherit from that one.

This controller is useful for changing header color, adding custom texts, logos, etc. and I would really like to make my Manage and Account controller inherit from it too to make my Web Application have the same "style" in every page.

The problem is that the Account and Manage controllers can't inherit from BaseController because they already have two constructors, one empty, and one with parameters ApplicationUserManager and ApplicationSignInManager.

If I try to inherit from BaseController it gives me errors on these two constructors saying that they don't have the required formal parameters that I should pass from BaseController.

There is no argument given that corresponds to the required formal parameter (1stParam) of BaseController.BaseController(Type1stParam, Type2ndParam, Type3rdParam, Type4thParam)

I've searched for a while on StackOverflow to find an answer but I couldn't find much so I decided to create this question.

This is the custom controller.

public abstract class BaseController : Controller
    {
        readonly Servix2Repo.IUtente Utente;
        readonly Servix2Repo.IMenu Menu;
        readonly Servix2Repo.IAzienda Azienda;
        readonly ServixMVCModel _context;

        public BaseController(ServixMVCModel context, Servix2Repo.IUtente utenteRepo, Servix2Repo.IMenu menuRepo, Servix2Repo.IAzienda aziendaRepo)
        {
            _context = context;
            this.Utente = utenteRepo;
            this.Menu = menuRepo;
            this.Azienda = aziendaRepo;
        }

        (Other methods)

    }

This is one of the two controllers that I have problems with. I get the error on the empty constructor and the last one.

public class AccountController : BaseController
    { 

        private ApplicationSignInManager _signInManager;
        private ApplicationUserManager _userManager;

        public AccountController(ServixMVCModel context, Servix2Repo.IUtente utenteRepo, Servix2Repo.IMenu menuRepo, Servix2Repo.IAzienda aziendaRepo) : base(context, utenteRepo, menuRepo, aziendaRepo)
        {
        }

        public AccountController()
        {
        }

        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
        {
            UserManager = userManager;
            SignInManager = signInManager;
        }

        (Other methods)

    }

Thanks to anyone who is willing to help me and I'm sorry if I made some wording mistakes.


Solution

  • Fixed using Thangadurai's suggestion in the comments:

    In your BaseController, you do not have any default constructor that accepts no parameters. So, In all your AccountController constructors you should call the base constructor in the same way how you did it for the first constructor OR you must include one default constructor in the base class