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. }  
  • 相关阅读:
    jQuery 字符串拼接
    jQuery 筛选器 链式编程操作
    jQuery 常用操作
    jQuery介绍 常用选择器
    枚举-增加BOOL类型
    枚举-判断一年中第 125 天是工作日, 还是休息日?假设一年 365 天, 新年第一天是星期一。
    判断大小端序
    信息存储,共用体的应用
    栈的自实现
    strlen,strcat,strcpy,strcpm 自实现
  • 原文地址:https://www.cnblogs.com/chaoshenzhu/p/7464230.html
Copyright © 2011-2022 走看看