【问题标题】:How to Ignore Properties at the Class Level for .Net Core Web API Bind Model如何忽略 .Net Core Web API 绑定模型的类级别的属性
【发布时间】:2022-01-20 16:21:19
【问题描述】:

有没有办法在 JsonInclude 之类的请求模型中为复杂类型指定包含或排除的属性?我认为必须为每个请求编写一个 POCO,并且必须为每个 POCO 上的每个属性添加验证属性将是一场噩梦。

public class RegisterUserRequest
{
    //Ignore all other properties including their requirement attributes
    [JsonInclude("FirstName,LastName,Username,Password")] 
    public User user { get; set; }

    [Required]
    public string PasswordConfirmation { get; set; }
    
    [Required]
    public string Email { get; set; }
}

【问题讨论】:

    标签: json asp.net-core-webapi


    【解决方案1】:

    【讨论】:

    • 谢谢,基本的忽略条件会起作用。我可以使用 [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] 并在实例化时设置默认值
    • @JGDev,你也可以使用 DefaultContractResolver 来动态序列化。这里有什么问题吗?
    • 不,那将是理想的。在这种情况下,我没有看到 DefaultContractResolver 在哪里做的。你有例子吗?
    猜你喜欢
    • 2020-09-27
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多