我的应用程序需要(几乎是默认的)JSON 序列化设置:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
});
仅对于一个控制器,我需要对两个输入使用不同的命名策略(我使用模型绑定[FromBody] myComplexObject
和输出
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
我的问题几乎与Web API 相同:Configure JSON serializer settings on action or controller level除了我要求的 AspNet Core 2.2+IControllerConfiguration
已经不存在了。
Core 2.1+ 等效问题在这里有一个响应:Configure input/output formatters on controllers with ASP.NET Core 2.1
那里的答案似乎有些零散或不完整——很难想象没有更简单的方法可以实现这一目标。有人知道如何在单个控制器中对所有输入和输出使用 DefaultContractResolver 吗?