c# - ASP Net Core - 通用页面模型
问题描述
我开始学习使用 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 类一样为页面模型使用通用参数?我认为这对于其他一些情况也可能非常有用。
解决方案
我想我找到了解决问题的方法。内部类似乎由 PageModel 约定初始化,可以在 IdentityPageModelConvention 源文件中找到:
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 : PageModel
推荐阅读
- popup - tvOS 上的 Toast/弹出窗口
- cordova - Meteor/Cordova:帐户密码在登录/注册时破坏应用程序
- java - 如何修复“App Engine SDK 已过期”?
- javascript - 如何使用 javascript 或 jquery 在程序上调整具有像素精度的媒体查询?
- angular - 升级到最新版本后,ng-select 下拉菜单在 UI 上中断 - Angular 6
- java - 使用 Spring Security 保存多部分表单数据给出错误 HTTP 状态 405 - 不支持请求方法“POST”
- c# - 在到达最后一行索引时将新项目添加到 itemsource 或 Datagrid 中
- javascript - Loopback 4 测试前的空数据库
- ios - 如果变量相等,则遍历两个自定义数组并设置值 Swift
- reactjs - jsdoc中的React导入tsint星号必须对齐