ASP.NET 核心中的位置记录属性

Positional record attributes in ASP.NET Core

感谢审稿人找到重复项:

.NET 5.0 Web API won't work with record featuring required properties也有关系。

我正在尝试理解为什么 3. 位置记录属性在下面不起作用。

这是一个新的 ASP.NET Core Razor Pages Web 应用程序,源代码在此处 https://github.com/djhmateer/record-test

https://daveabrock.com/2020/11/18/simplify-api-models-with-records 似乎已经与 API 一起工作了。

public class LoginModelNRTB : PageModel
{
    [BindProperty]
    // there will never be an InputModel on get
    // but if we set to nullable InputModel then the cshtml will produce dereference warnings
    // 
    // so this will get rid of the warnings as we are happy we will never get dereferences on the front
    // ie we are happy the underlying framework will not produce null reference exceptions
    public InputModel Input { get; set; } = null!;


    // 1. Original Class which works
    //public class InputModel
    //{
    //    // don't need required as Email property is non nullable
    //    //[Required]
    //    // makes sure a regex fires to be in the correct email address form
    //    [EmailAddress]
    //    // there should always be an email posted, but maybe null if js validator doesn't fire
    //    // we are happy the underlying framework handles it, 
    //    public string Email { get; set; } = null!;

    //    // When the property is called Password we don't need a [DataType(DataType.Password)]
    //    //[DataType(DataType.Password)]
    //    public string Password { get; set; } = null!;

    //    [Display(Name = "Remember me?")]
    //    public bool RememberMe { get; set; }
    //}

    // 2. Record which works
    //public record InputModel
    //{
    //    [EmailAddress]
    //    public string Email { get; init; } = null!;

    //    [DataType(DataType.Password)]
    //    public string PasswordB { get; init; } = null!;

    //    [Display(Name = "Remember me?")]
    //    public bool RememberMe { get; init; }
    //}

    // 3. Positional record attributes not being picked up
    public record InputModel(
        string Email,
        [DataType(DataType.Password)] string PasswordB,
        [Display(Name = "Remember me?")] bool RememberMe);


    public void OnGet() { }

    public IActionResult OnPost()
    {
        if (ModelState.IsValid)
        {
            // Input property of type InputModel is bound because of the [BindProperty] attribute
            Log.Information($"Success! {Input}");
            return LocalRedirect("/");
        }

        Log.Information($"Failure on ModelState validation {Input}");
        return Page();
    }
}

来自proposal specification

Attributes can be applied to the synthesized auto-property and its backing field by using property: or field: targets for attributes syntactically applied to the corresponding record parameter.

下一步试试:

public record InputModel(
    string Email,
    [property:DataType(DataType.Password)] string PasswordB,
    [property:Display(Name = "Remember me?")] bool RememberMe);

基于sharplab.io反编译,应该为生成的属性发出属性。

如果没有此目标,属性将作为相应记录上的属性发出 constructor parameter