ami*_*jam 7 asp.net asp.net-mvc asp.net-core
任何人都知道如何为asp.net vnext设置camelCase JSON序列化?在Startup.cs我有两个功能.
public void ConfigureServices(IServiceCollection services) {}
public void Configure(IApplicationBuilder app) {}
Run Code Online (Sandbox Code Playgroud)
如何设置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();
Run Code Online (Sandbox Code Playgroud)
--- 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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1055 次 |
| 最近记录: |