C# MVC中没有为此对象错误定义无参数构造函数
我想在AccountController上使用依赖项注入,但在登录时出现错误“没有为此对象定义无参数构造函数”。这是在登录时使用依赖项注入的真实方法吗?很多人在登录控制上使用这种方法C# MVC中没有为此对象错误定义无参数构造函数,c#,asp.net-mvc,dependency-injection,controller,inversion-of-control,C#,Asp.net Mvc,Dependency Injection,Controller,Inversion Of Control,我想在AccountController上使用依赖项注入,但在登录时出现错误“没有为此对象定义无参数构造函数”。这是在登录时使用依赖项注入的真实方法吗?很多人在登录控制上使用这种方法 public interface IAuthProvider { bool Authenticate(string username, string password); } public class AccountController : Controller { IAuthProvider a
public interface IAuthProvider {
bool Authenticate(string username, string password);
}
public class AccountController : Controller {
IAuthProvider authProvider;
public AccountController(IAuthProvider auth) {
authProvider = auth;
}
public ViewResult Login() {
return View();
}
[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl) {
if (ModelState.IsValid) {
if (authProvider.Authenticate(model.UserName, model.Password)) {
return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
} else {
ModelState.AddModelError("", "Incorrect username or password");
return View();
}
} else {
return View();
}
}
}
我猜你在使用统一。如果是这样,就不需要无参数构造函数。注册实例时,应使用。使用该统一可以解析正确的构造函数。请显示用于将
IAuthProvider
映射到实现的代码。