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 }
                );
            }
        }

    --

  • 相关阅读:
    phpExcel常用方法详解 F
    简单的图片变色方法 F
    TCP协议数据包及攻击分析
    你好世界
    团队项目 第一次作业
    NOIP提高组(2018)考试技巧及注意事项
    ACM常用模板数论
    ACM常用模板图论
    ACM常用模板数据结构
    I'm Coming
  • 原文地址:https://www.cnblogs.com/runliuv/p/10784232.html
Copyright © 2011-2022 走看看