我需要用camelcase格式化来自web api 2的json响应,所以我将以下行添加到了我的基本控制器类的构造函数中:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
这是可行的,但会导致dnn(它所基于的asp.net framework 4.5应用程序)出现一些问题。
我还尝试通过apicontroller的actionContext属性设置此值:
ActionContext.ControllerContext.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
但配置对象始终为空。
有没有办法只为webapi配置jsonformatters?我意识到我可以用属性来修改每个属性,这些属性改变了NeNoToSoT将输出JSON对象的情况,但我真的不想为所有的API资源都这样做。