简体   繁体   English

在 ASP.NET Core 3 中更改单个 ASP.NET Core API 控制器或单个操作的 System.Text.Json 序列化选项

[英]Change System.Text.Json Serialization Options of a single ASP.NET Core API controller or single Action in ASP.NET Core 3

I'm having two controller controllers: ControllerA and ControllerB.我有两个控制器控制器:ControllerA 和 ControllerB。 The base class of each controller is ControllerBase.每个控制器的基类是 ControllerBase。

The ControllerA needs to deserialize JSON in the default option ControllerA 需要在默认选项中反序列化 JSON

JsonSerializerOptions.IgnoreNullValues = false;

The ControllerB needs to deserialize JSON with option ControllerB 需要使用选项反序列化 JSON

JsonSerializerOptions.IgnoreNullValues = true;

I know how to set this option global in Startup.cs我知道如何在 Startup.cs 中全局设置此选项

services.AddControllers().AddJsonOptions( options => options.JsonSerializerOptions.IgnoreNullValues = true);

But how to set specific deserialize options to Controller or Action?但是如何为 Controller 或 Action 设置特定的反序列化选项? (ASP.NET Core 3 API) (ASP.NET Core 3 API)

As suggested by Fei Han, the straight-forward answer is to use on ControllerB the attribute NullValuesJsonOutput :正如 Fei Han 所建议的,直接的答案是在 ControllerB 上使用NullValuesJsonOutput属性:

public class NullValuesJsonOutputAttribute : ActionFilterAttribute
{
    private static readonly SystemTextJsonOutputFormatter Formatter = new SystemTextJsonOutputFormatter(new JsonSerializerOptions
    {
        IgnoreNullValues = true
    });

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        if (context.Result is ObjectResult objectResult)
            objectResult.Formatters.Add(Formatter);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改单个 ASP.NET Core 控制器的 JSON 序列化设置 - Change the JSON serialization settings of a single ASP.NET Core controller ASP.NET Core 3.0 System.Text.Json Camel Case连载 - ASP.NET Core 3.0 System.Text.Json Camel Case Serialization 更改单个 ASP.NET 内核 controller 的 JSON 反序列化/序列化策略 - Change the JSON deserialization/serialization policy for single ASP.NET Core controller ASP.NET Core 3.1 如何让 controller 使用 System.Text.Json 进行序列化和反序列化 - ASP.NET Core 3.1 How can I get the controller to use System.Text.Json to serialize and deserialize 使用 System.Text.JSON 和 ASP.NET 核心 3:决定漂亮的 JSON Z78E6221F6393D14CEDZ 上端点? - using System.Text.JSON with ASP.NET Core 3: decide about pretty JSON output on endpoint? 如何更改 ASP.NET Core API 中的默认控制器和操作? - How to change the default controller and action in ASP.NET Core API? 使用 System.Text.Json 在 ASP.NET Core 3.0 中格式化 DateTime - Formatting DateTime in ASP.NET Core 3.0 using System.Text.Json ASP.NET Core - System.Text.Json:如何拒绝负载中的未知属性? - ASP.NET Core - System.Text.Json: how to reject unknown properties in payload? 在 ASP.NET Core 2.1 中使用 System.Text.Json 作为默认序列化程序 - Using System.Text.Json as Default Serializer in ASP.NET Core 2.1 在 ASP.Net Core 3.x DI 中配置 System.Text.Json Camel Case - Configuring System.Text.Json Camel Case in ASP.Net Core 3.x DI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM