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 的属性
};
}
}
} 返回的json数据中过滤掉值是null的属性
最新推荐文章于 2021-08-25 17:45:44 发布
本文介绍如何通过配置WebAPI来实现默认返回JSON格式的数据,并提供XML格式数据的支持选项。此外,还介绍了如何设置JSON序列化时忽略值为null的属性。
1619

被折叠的 条评论
为什么被折叠?



