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. }  
  • 相关阅读:
    阿里云ssh断开处理办法
    OSSIM安装使用教程(OSSIM-5.6.5)
    MySQL字符串列与整数比较
    Linux获取so/ko文件版本号教程
    Linux服务器后门自动化查杀教程
    最强半自动化抓鸡工具打造思路
    渗透测试报告中的那些名词解释
    ELK+MySQL出现大量重复记录问题处理
    Python3+SQLAlchemy不使用字段名获取主键值教程
    Python3+SQLAlchemy+Sqlite3实现ORM教程
  • 原文地址:https://www.cnblogs.com/chaoshenzhu/p/7464230.html
Copyright © 2011-2022 走看看