我的Web API中默认使用camelCase,但我需要在特定控制器(或特定的http操作)中禁用此设置。这可能吗?
答案 0 :(得分:3)
我用这个答案解决了这个问题:https://stackoverflow.com/a/19959579/3621839
我改为反过来,只有:
using System;
using System.Linq;
using System.Web.Http.Controllers;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Serialization;
public class DisableCamelCaseSerialization : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single();
controllerSettings.Formatters.Remove(formatter);
formatter = new JsonMediaTypeFormatter
{
SerializerSettings = { ContractResolver = new DefaultContractResolver() }
};
controllerSettings.Formatters.Add(formatter);
}
}