任何人都知道如何为asp.net vnext设置camelCase JSON序列化?在Startup.cs
我有两个功能。
public void ConfigureServices(IServiceCollection services) {}
public void Configure(IApplicationBuilder app) {}
如何设置JSON.net使用camelCase?在之前的asp.net版本中,我有一个GlobalConfiguration
对象,我可以像这样更改
var config = GlobalConfiguration.Configuration;
// Replace the default JsonFormatter with our custom one
var index = config.Formatters.IndexOf(config.Formatters.JsonFormatter);
config.Formatters[index] = new JsonCamelCaseFormatter();
--- UPDATE
谢谢Kiran。这是与当前版本的asp.net一起使用的更新解决方案。
services.AddMvc().Configure<MvcOptions>(options =>
{
int position = options.OutputFormatters.FindIndex(f =>
f.Instance is JsonOutputFormatter);
var settings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var formatter = new JsonOutputFormatter();
formatter.SerializerSettings = settings;
options.OutputFormatters.Insert(position, formatter);
});