zoukankan      html  css  js  c++  java
  • WebApi——json返回多了 k_BackingField

    产生原因:

    model类添加了    [System.Serializable]

    解决方案:

    xxxxx.WebApiApp_StartWebApiConfig.cs的Register函数中添加如下代码

                config.Formatters.XmlFormatter.UseXmlSerializer = true;
                config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("$Format", "xml", "application/xml"));
                config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("OutputFormat", "xml", "application/xml"));
                config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    
                var serializerSettings =
                  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
                var contractResolver =
                  (DefaultContractResolver)serializerSettings.ContractResolver;
                contractResolver.IgnoreSerializableAttribute = true;
                //config.Formatters.Insert(0, new JsonpFormatter());
                config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
                config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("$Format", "json", "application/json"));
                config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("OutputFormat", "json", "application/json"));
    

      

  • 相关阅读:
    Redmine:数据库的账号密码配置
    EF:自定义Oracle的映射类型
    WCF:调用超时
    ibator使用心得
    Mantis部署步骤
    Detach之后的POCO如何Attach以及LoadProperty
    快速迭代与原型开发
    .net framework 4.0无法在Win7下安装的解决办法
    MVC和MVP的一些思考
    从类模型转换到数据库表结构的思考
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/7325421.html
Copyright © 2011-2022 走看看