[英]How to Ignore Properties at the Class Level for .Net Core Web API Bind Model
有沒有辦法像 JsonInclude 一樣在請求 model 中為復雜類型指定包含或排除的屬性? 我認為必須為每個請求編寫一個 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; }
}
您可以使用 DefaultContractResolver
給定的兩個鏈接可以幫助您。
https://www.newtonsoft.com/json/help/html/conditionalproperties.htm
此外,您可以在給定鏈接中的 ReadOnly、Empty 等條件下覆蓋基本忽略屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.