Reputation: 101
I'm new to MVC and C# and am stuck on this error. I don't know how to correct it. Any help would be appreciated.
I'm trying to bring the customer repository into the AccountController so that when creating a user you can associate it with a customer from the drop down list on the register user view.
I'm getting the error on this line in my controller.
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
The error is: account controller does not contain a constructor that takes 1 arguments I've tried a number of corrections and am stuck.
public AccountController(UserManager<ApplicationUser> userManager, ICustomerRepository customerRepository)
{
UserManager = userManager;
customerrepository = customerRepository;
}
Thanks
Upvotes: 2
Views: 664
Reputation: 435
It looks like you have need to supply an argument for the customerRepository parameter in your AccountController constructor.
It might be a better solution to create properties in your controller that can get generate these properties if they are not supplied in the constructor:
Here's an example of the user manager:
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager {
get {
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set {
_userManager = value;
}
}
Upvotes: 0
Reputation: 11957
You have 2 constructors in AccountController
.
The 1st one is calling the 2nd one.
The 2nd one expects 2 parameters: a UserManager<ApplicationUser>
, and an ICustomerRepository
.
It is erroring because you are only passing in one thing from the 1st one into the 2nd one.
You need to pass another parameter into the 2nd constructor.
i.e. guessing you meant to do something like this:
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())),
new CustomerRepository())
{
}
Upvotes: 1
Reputation: 101192
Looks like you only have one constructor:
public AccountController
{
UserManager<ApplicationUser> _manager ;
public AccountController()
{
this._manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
}
}
You should only use : this(xxx)
when there are more than one constructor and you do not want to duplicate code between the constructors.
public AccountController
{
UserManager<ApplicationUser> _manager ;
public AccountController()
:this(new ApplicationDbContext()) //calls the other constructor with a default context
{
}
public AccountController(ApplicationDbContext context)
{
this._manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context);
}
}
Upvotes: 0