更新
感谢所有的答案。我现在正在一个新项目上工作,看起来我终于找到了问题的原因:实际上以下代码是有问题的:
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,但事实并非如此。
这现在到处都在用......我能不能成为第一个说"wtf"的人?或者也许应该是"为什么他们要这样做?"
以下是原始问题
人们可能会认为这是一个简单的配置设置,但是我已经追寻了太久了。
我查看了各种解决方案和答案:
https://gist.github.com/rdingwall/2012642
似乎不适用于最新的WebAPI版本......
以下方法似乎无效-属性名称仍然是PascalCased。
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子对象(嵌套对象,子对象)似乎是一个不太令人满意但可行的答案,直到我意识到这些属性需要添加到生成的代码中,因为我们正在使用linq2sql...
是否有自动完成这个步骤的方法?这个'讨厌的问题'困扰我很久了。
JsonSerializer
的位置。https://dev59.com/K2Yr5IYBdhLWcg3w7uhx - Aron