筆記下輸出 JSON 回應時不顯示 NULL 屬性

結論
public class CreateTransactionDto
{
public bool IsSuccess { get; set; }
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ApiException Ex { get; set; }
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public CreateTransactionResult Result { get; set; }
}
備註
如果像 int 因為不會有 null 的這種可以用 Default
JsonIgnoreCondition.WhenWritingDefault
說明
- 回傳結果成功時會顯示
Result
但不會顯示Ex
- 回傳結果失敗時會顯示
Ex
但不會顯示Result
參照
JsonIgnoreAttribute Class (System.Text.Json.Serialization) | Microsoft Learn
