WebApi——json返回多了 k_BackingField

产生原因:

model类添加了    [System.Serializable]

 

 

解决方案:

xxxxx.WebApi\App_Start\WebApiConfig.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"));

  

 

posted @ 2017-08-09 15:11  PanPan003  阅读(1079)  评论(0编辑  收藏  举报