我开始学习使用ASP Net Core进行编程。在花一些时间学习Asp Net Core Identity时,我想实现一个自己的登录页面以供学习之用。不幸的是,如果你想使用一个包含泛型参数的页面模型,比如Asp Net Core Identity的Login.cshtml.cs,我现在有一些问题需要理解依赖注入是如何工作的。在源代码中,有两个类派生自登录页面的页面模型:
[AllowAnonymous]
[IdentityDefaultUI(typeof(LoginModel<>))]
public abstract class LoginModel : PageModel和
internal class LoginModel<TUser> : LoginModel where TUser : class我读到SignInManager类在Identity中处理登录和注销过程。但因此我想我必须使用内部类。但我不明白Asp Net Core标识符如何使用内部类而不是抽象类,或者它确实是这样做的?!
即使在剃刀页面中,也只有抽象类被用作模型:
@page
@model LoginModel
@{
ViewData["Title"] = "Log in";
}有没有人可以向我解释一下,为了能够像内部LoginModel类那样使用页面模型的泛型参数,我必须做些什么?我认为这对其他一些情况也非常有用。
发布于 2019-06-23 04:20:06
我想我找到了我的问题的解决方案。内部类似乎是由可以在IdentityPageModelConvention源文件中找到的PageModel约定初始化的:
public void Apply(PageApplicationModel model)
{
var defaultUIAttribute = model.ModelType.GetCustomAttribute<IdentityDefaultUIAttribute>();
if (defaultUIAttribute == null)
{
return;
}
ValidateTemplate(defaultUIAttribute.Template);
var templateInstance = defaultUIAttribute.Template.MakeGenericType(typeof(TUser));
model.ModelType = templateInstance.GetTypeInfo();
}此方法似乎通过抽象LoginModel类中定义的IdentityDefaultUI属性来确定具有TUser泛型属性的内部类:
[IdentityDefaultUI(typeof(LoginModel<>))]
public abstract class LoginModel : PageModelhttps://stackoverflow.com/questions/56711839
复制相似问题