[英]No parameterless constructor defined for this object error in MVC
我想在AccountController上使用依赖注入,但是在登录时出现此错误“此对象未定义无参数的构造函数”。这是在loginig上使用依赖注入的真实方法。很多人在登录控制上使用这种方法。
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();
}
}
}
我猜您正在使用Unity。 如果是这样,则不需要无参数的构造函数。 注册实例时,应使用InjectionConstructor 。 这样,Unity可以解析正确的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.