原文:http://blog.csdn.net/xxj_jing/article/details/49508557

版权声明:本文为博主原创文章,未经博主允许不得转载。

.net mvc web api 返回 json 内容时,好多属性为null的没必要下发。

下面看下怎么过滤值为null的属性

.响应内容(过滤前)

{"msg":"初始化成功!","code":"","success":true,data:null}

.响应内容(过滤后)

{"msg":"初始化成功!","code":"","success":true}

[csharp] view plain copy
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 的属性
};
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

.net mvc web api 返回 json 内容时,好多属性为null的没必要下发。

下面看下怎么过滤值为null的属性

 

1.响应内容(过滤前)

{"msg":"初始化成功!","code":"","success":true,data:null}

 

2.响应内容(过滤后)

{"msg":"初始化成功!","code":"","success":true}

[csharp] view plain copy

 
  1. using System.Net.Http.Formatting;
  2. using System.Web.Http;
  3. namespace xxxxxxx
  4. {
  5. public static class WebApiConfig
  6. {
  7. public static void Register(HttpConfiguration config)
  8. {
  9. ......
  10. //默认返回 json
  11. GlobalConfiguration.Configuration.Formatters
  12. .JsonFormatter.MediaTypeMappings.Add(
  13. new QueryStringMapping("datatype", "json", "application/json"));
  14. //返回格式选择
  15. GlobalConfiguration.Configuration.Formatters
  16. .XmlFormatter.MediaTypeMappings.Add(
  17. new QueryStringMapping("datatype", "xml", "application/xml"));
  18. //json 序列化设置
  19. GlobalConfiguration.Configuration.Formatters
  20. .JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
  21. {
  22. NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore //设置忽略值为 null 的属性
  23. };
  24. }
  25. }
  26. }

.net mvc web api 返回 json 内容,过滤值为null的属性的更多相关文章

  1. C# .net mvc web api 返回 json 内容,过滤值为null的属性

    在WebApiConfig.Register 中增加一段 #region 过滤值为null的属性 //json 序列化设置 GlobalConfiguration.Configuration.Form ...

  2. .net mvc web api 返回 json 内容时过滤值为null的属性

    1.响应内容(过滤前) {"msg":"初始化成功!","code":"","success":tr ...

  3. MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。

    原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...

  4. ASP.NET WEB API 返回JSON 出现2个双引号问题

    前言          在使用ASP.NET WEB API时,我想在某个方法返回JSON格式的数据,于是首先想到的就是手动构建JSON字符串,如:"{\"result\" ...

  5. .Net Web Api返回Json数据中原对象变量名大小写问题

    这两天在工作中使用SignalR的WebSocket做数据实时传递的功能开发,在后端主动向前端广播数据以Json传递时,前端获取的Json中对应类的变量名首字母默认传递的是大写.而前端一直获取到的后台 ...

  6. Web API返回JSON数据

    对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...

  7. web Api 返回json 的两种方式

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  8. Web API 返回json文件的2中不用方式

    //方法一:直接返回序列化后的json文件 public static HttpResponseMessage ConvertToJson(this Object obj) { String str= ...

  9. Asp.net Web API 返回Json对象的两种方式

    这两种方式都是以HttpResponseMessage的形式返回, 方式一:以字符串的形式 var content = new StringContent("{\"FileName ...

随机推荐

  1. bootstrap-fileinput简单完整列子

    文件夹结构 版本都是3.x <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...

  2. bzoj3743: [Coci2015]Kamp

    首先树dp求出一个点的答案 然后再一遍dfs换根(是叫做换根吗.. 详见代码 #include <iostream> #include <cstdio> #include &l ...

  3. c#读取excel

    Provider根据实际EXCEL的版本来设置,推荐使用ACE接口来读取.需要Access database Engine. 注意修改注册表以下两项的值为0.否则导入EXCEL当单元格内字符长度超过2 ...

  4. matplotlib 基础

    plt.figure(2) #创建图表2 plt.figure(1) #创建图表1 ax1=plt.subplot(211) # 在上面 最近的 图表1上 创建子图1 ax2=plt.subplot( ...

  5. 【BZOJ-4688】One-Dimensional 矩阵乘法

    4688: One-Dimensional Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 19  Solved: 12[Submit][Status] ...

  6. 【BZOJ-4386】Wycieczki DP + 矩阵乘法

    4386: [POI2015]Wycieczki Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 197  Solved: 49[Submit][Sta ...

  7. oracle 常用sql

    -- 在sql中只有别名时用的是双引号select employee_id,last_name,12*salary as yearSalary from employees; select last_ ...

  8. ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet

    严重: Context initialization failedorg.springframework.beans.factory.BeanDefinitionStoreException: Fai ...

  9. 【uoj147】NOIP2015—斗地主

    http://uoj.ac/problem/147 (题目链接) 题意 打牌... Solution 其实很简单的搜索,当年还是太年轻了.稍微想一想,顺子肯定是要先打掉的,因为顺子所包含的牌最多,所以 ...

  10. Python之禅+八荣八耻

    Python之禅 (The Zen of Python):是Python语言的指导原则,可以在Python命令行输入import this显示. import this >>> Th ...