UPDATE
谢谢你的回答.我正在进行一个新项目,看起来我终于弄清了这一点:看起来实际上应该归咎于以下代码:
public static HttpResponseMessage GetHttpSuccessResponse(object response, HttpStatusCode code = HttpStatusCode.OK)
{
return new HttpResponseMessage()
{
StatusCode = code,
Content = response != null ? new JsonContent(response) : null
};
}
在别处
public JsonContent(object obj)
{
var encoded = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } );
_value = JObject.Parse(encoded);
Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
我忽略了看似无害的JsonContent,假设它是WebAPI,但不是.
这是用everywhere...我能第一个说吗,wtf?或者应该是"他们为什么这么做?"
original question follows
有人会认为这是一个简单的配置设置,但我已经逃避太久了.
我研究了各种解决方案和答案:
https://gist.github.com/rdingwall/2012642个
似乎不适用于最新的WebAPI版本...
以下方法似乎不起作用-属性名称仍然使用PascalCase.
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;
json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Mayank在这里的答案是:CamelCase JSON WebAPI Sub-Objects (Nested objects, child objects)似乎是一个不令人满意但可行的答案,直到我意识到这些属性必须添加到生成的代码中,因为我们正在使用linq2sql...
有没有办法自动做到这一点?这个"肮脏"已经困扰我很长一段时间了.