[RESOLVED]How Add Method in Constructer

How to add AccountRepository() method bellow constructor. Please help me ? I didnt added

<div>

public class AccountController : Controller
{
        public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }
}

Could you provide a bit more detail about what you are trying to accomplish?

If you wanted to add a particular Controller Action method, you would simply need to define it as follows :

public class AccountController : Controller
{
        // Constructor
        public AccountController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {

        }

        // Define your method here
        public void AccountRepository()
        {
               // Do something here
        }
}
public class AccountController : Controller
{
        public AccountController()
            : -->  Where Can I Insert and how -> this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }
}


I added bellow have a error I dont know Where can I  insert

public class AccountController : Controller
{
        public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())new AccountRepository()))
        {
        }
}


 

Generally in an instance like this, you would have your specific Repository defined at the Controller level and set using the constructor :

public class AccountController : Controller
{
        public AccountRepository Repository{ get; private set; }

        public AccountController(): this(new AccountRepository()))
        {
              
        }

        public AccountController(AccountRepository accountRepository)
        {
              Repository = accountRepository;
        }
}
        

You can see a basic example similar to the example provided in your initial code within

this ASP.NET Identity snippet
.

Thank you 

public class AccountController : Controller
    {
        private ApplicationUserManager _userManager;
 
        public AccountController()
            : this(
                new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())),
                new AccountRepository())
        {
        }
 
        public AccountController(UserManager<ApplicationUser> userManager, AccountRepository accountRepository)
        {
            // TODO: Complete member initialization
            this.userManager = userManager;
            this.accountRepository = accountRepository;
        }
    }

Leave a Reply