在POST之后访问构造函数时(在GET期间没有问题),我收到此错误。
{”无法创建类型'MyWebsite.Website.Main.Areas.Identity.Pages.Account.LoginWith2faModel + IInputModel'的实例。模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数。
在我的StartUp.cs
中,我显然已经为创建InputModel实例的接口添加了Transient Service
。我根本不明白为什么我会收到该错误。是因为interface
和class
都在我的LoginWith2faModel类中定义了吗?
我问,因为在另一个项目中,我有与此类似的设置,并且没有问题。同样,我有一个使用interface
的构造函数,并将interface
的具体实现传递给该类并在构造函数中对其进行初始化。这和那之间的唯一区别是asp.net核心的DI。非常感谢您的帮助!
services.AddTransient<LoginWith2faModel.InputModel>();
//previously I had the above set to:
//services.AddTransient<LoginWith2faModel.IInputModel,LoginWith2faModel.InputModel>();
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ILogger<LoginWith2faModel> _logger;
public LoginWith2faModel(SignInManager<IdentityUser> signInManager, ILogger<LoginWith2faModel> logger, IInputModel inputModel)
{
_signInManager = signInManager;
_logger = logger;
Input = inputModel;
}
[BindProperty]
public IInputModel Input { get; set; }
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
public interface IInputModel
{
string TwoFactorCode { get; set; }
bool RememberMachine { get; set; }
}
public class InputModel : IInputModel
{
public string TwoFactorCode { get; set; }
public bool RememberMachine { get; set; }
}