我希望所有控制器结果上的驼峰式 JSON 期望一个返回字典的特定控制器
我试过这个
public class PascalCaseConfigAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings config,
HttpControllerDescriptor controllerDescriptor)
{
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().Single();
jsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();
}
}
但这会改变全局配置,因此在使用该属性调用任何控制器之后,我们将回到帕斯卡案例。
我如何才能修复默认为驼峰式大小写并显式修复某个控制器的 Paascal 大小写?
编辑:这可行,但感觉有点落后
public class PascalCaseConfigAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings config,
HttpControllerDescriptor controllerDescriptor)
{
var formatter = config.Formatters.OfType<JsonMediaTypeFormatter>().Single();
config.Formatters.Remove(formatter);
formatter = new JsonMediaTypeFormatter();
formatter.SerializerSettings.ContractResolver = new DefaultContractResolver();
config.Formatters.Add(formatter);
}
}