ASP.NET Core 中的位置记录属性 [英] Positional record attributes in ASP.NET Core

查看:81
本文介绍了ASP.NET Core 中的位置记录属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢审阅者找到重复项:How记录类的目标属性?

Thank you to the reviewers for finding the duplicate: How do I target attributes for a record class?

.NET 5.0 WebAPI 不适用于具有所需属性的记录 也是相关的.

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

I'm trying to understand why 3. Positional record attributes don't work below.

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

It is a new ASP.NET Core Razor Pages Web App with source here https://github.com/djhmateer/record-test

https://daveabrock.com/2020/11/18/simplify-api-models-with-records 似乎让它与 API 一起工作.

https://daveabrock.com/2020/11/18/simplify-api-models-with-records seems to have got it working with an 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
    // https://stackoverflow.com/a/54973095/26086
    // 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();
    }
}

推荐答案

来自 提案规范:

属性可以通过使用 property:field: 目标在语法上应用于相应记录参数的属性来应用于合成的自动属性及其支持字段.

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 反编译,则属性应该被射出的生成的属性.

Based on sharplab.io decompilation, the attributes should be emitted for generated properties.

如果没有这个目标属性时发出作为属性上相应的记录构造符参数.

Without this target the attribute gets emitted as attribute on corresponding record constructor parameter.

这篇关于ASP.NET Core 中的位置记录属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆