欢迎光临散文网 会员登陆 & 注册

Configuring JSON options in ASP.NET Core

2023-06-03 08:01 作者:Tuple_元组  | 我要投稿

如何格式化 JSON 对象中属性的大小写,不同的场景有如下变化:

In an ASP.NET Core application, you can configure the JSON serializer options used by controllers using the AddJsonOptions method:

This works well for controllers. .NET 6 introduced minimal APIs for hosting and routing in web applications. This is an easier way to create small web APIs. This new model does not use controllers, so you cannot use the AddJsonOptions method. AddJsonOptions configures Microsoft.AspNetCore.Http.Json.JsonOptions using the Dependency Injection (source). So, you can do the same directly. In the following example, I configure the JSON serializer options using the Configure method:

The JSON serializer uses the configuration settings

In .NET 6 preview 7, you can also provide the JSON serializer options using Results.Json(object, JsonSerializerOptions) (source):

ref: https://www.meziantou.net/configuring-json-options-in-asp-net-core.htm

Configuring JSON options in ASP.NET Core的评论 (共 条)

分享到微博请遵守国家法律