.Net Core WebAPI 序列化时忽略空值字段

特定实体

    public class APIResponseModel
    {
        /// <summary>
        /// 返回标识,200:成功,其它:失败
        /// </summary>
        public string Code { get; set; } = "201";
        /// <summary>
        /// 返回描述
        /// </summary>
        public string Msg { get; set; }
        /// <summary>
        /// 返回数据(DES加密后)
        /// </summary>
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string Data { get; set; }
    }

全局

builder.Services.AddControllers().AddJsonOptions(options => {
                options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
            }); 

 

posted @ 2023-02-17 08:25  拼博之路  阅读(179)  评论(0编辑  收藏  举报