Configuring JSON options in ASP.NET Core
如何格式化 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