问题描述
json序列化程序设置,但我不使用
。那么如何设置全局json序列化设置? asp.net核心3
中的AddMvc()
json serializer settings for legacy asp.net core applications were set by adding AddMvc().AddJsonOptions()
, but I don't use AddMvc()
in asp.net core 3
. So how can I set global json serialization settings?
推荐答案
AddMvc
返回一个实现,它具有相应的扩展方法。新型方法 AddControllers
, AddControllersWithViews
和 AddRazorPages
还返回 IMvcBuilder
实现。以与链接 AddMvc
相同的方式链接它们:
AddMvc
returns an IMvcBuilder
implementation, which has a corresponding AddJsonOptions
extension method. The new-style methods AddControllers
, AddControllersWithViews
, and AddRazorPages
also return an IMvcBuilder
implementation. Chain with these in the same way you would chain with AddMvc
:
services.AddControllers()
.AddJsonOptions(options =>
{
// ...
});
请注意,此处的个选项
Json.NET,但对于较新的 System.Text.Json
API。如果您仍然想使用Json.NET,请参见
Note that options
here is no longer for Json.NET, but for the newer System.Text.Json
APIs. If you still want to use Json.NET, see tymtam's answer
这篇关于如何在ASP.NET Core 3中设置JSON序列化程序设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!