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

    在WebApiConfig。Register 中增加一段

    #region 过滤值为null的属性


    //json 序列化设置
    GlobalConfiguration.Configuration.Formatters
    .JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
    {
    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore //设置忽略值为 null 的属性
    };


    #endregion

    --

    --

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API 配置和服务
    
    
                #region 过滤值为null的属性
                //json 序列化设置
                GlobalConfiguration.Configuration.Formatters
                    .JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
                    {
                        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore //设置忽略值为 null 的属性
                    };
                #endregion
    
                // Web API 路由
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }

    --

  • 相关阅读:
    006_02SQLite_OpenHelper
    006_01SQLite_demo
    005_01XML_Serilizer
    004_05PullParser
    004_04SharedPreferences
    004_02文件读写模式
    004_01获取SD容量
    003_01电话拨号器
    maven项目中的pom.xml
    ORACLE提示表名无效
  • 原文地址:https://www.cnblogs.com/runliuv/p/10784232.html
Copyright © 2011-2022 走看看