簡體   English   中英

ASP Net Core-通用頁面模型

[英]ASP Net Core - Generic Page Models

我開始學習使用ASP Net Core進行編程。 在花一些時間使用Asp Net Core Identity時,我想實現自己的登錄頁面以供學習。 不幸的是,現在,如果您要使用包含Asp Net Core Identity的Login.cshtml.cs這樣的通用參數的Page Model,那么我有一些問題需要了解依賴注入的工作原理。 在源代碼中,有兩個類是從登錄頁面的頁面模型派生的:

[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 Attribute確定具有TUser泛型屬性的內部類:

[IdentityDefaultUI(typeof(LoginModel<>))]
public abstract class LoginModel : PageModel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM