zoukankan      html  css  js  c++  java
  • .net mvc web api 返回 json 内容时过滤值为null的属性

    1.响应内容(过滤前)

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

     

    2.响应内容(过滤后)

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

      1. using System.Net.Http.Formatting;  
      2. using System.Web.Http;  
      3.   
      4. namespace xxxxxxx  
      5. {  
      6.     public static class WebApiConfig  
      7.     {  
      8.         public static void Register(HttpConfiguration config)  
      9.         {  
      10.         ......  
      11.   
      12.             //默认返回 json  
      13.             GlobalConfiguration.Configuration.Formatters  
      14.                 .JsonFormatter.MediaTypeMappings.Add(  
      15.                 new QueryStringMapping("datatype", "json", "application/json"));  
      16.             //返回格式选择  
      17.             GlobalConfiguration.Configuration.Formatters  
      18.                 .XmlFormatter.MediaTypeMappings.Add(  
      19.                 new QueryStringMapping("datatype", "xml", "application/xml"));  
      20.             //json 序列化设置  
      21.             GlobalConfiguration.Configuration.Formatters  
      22.                 .JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()  
      23.                 {  
      24.                     NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore //设置忽略值为 null 的属性  
      25.                 };  
      26.         }  
      27.     }  
      28. }  
  • 相关阅读:
    剑指 Offer 06. 从尾到头打印链表
    剑指 Offer 05. 替换空格
    剑指 Offer 04. 二维数组中的查找
    剑指 Offer 03. 数组中重复的数字
    leetcode刷题笔记328题 奇偶链表
    leetcode刷题笔记324题 摆动排序 II
    leetcode刷题笔记321题 拼接最大数
    leetcode刷题笔记326题 3的幂
    20210301日报
    20210227日报
  • 原文地址:https://www.cnblogs.com/chaoshenzhu/p/7464230.html
Copyright © 2011-2022 走看看