返回的json数据中过滤掉值是null的属性

论坛 期权论坛 脚本     
匿名技术用户   2021-1-2 17:33   15   0
using System.Net.Http.Formatting;  
using System.Web.Http;  
  
namespace xxxxxxx  
{  
    public static class WebApiConfig  
    {  
        public static void Register(HttpConfiguration config)  
        {  
        ......  
  
            //默认返回 json  
            GlobalConfiguration.Configuration.Formatters  
                .JsonFormatter.MediaTypeMappings.Add(  
                new QueryStringMapping("datatype", "json", "application/json"));  
            //返回格式选择  
            GlobalConfiguration.Configuration.Formatters  
                .XmlFormatter.MediaTypeMappings.Add(  
                new QueryStringMapping("datatype", "xml", "application/xml"));  
            //json 序列化设置  
            GlobalConfiguration.Configuration.Formatters  
                .JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()  
                {  
                    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore //设置忽略值为 null 的属性  
                };  
        }  
    }  
}  

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP