【问题标题】:Process call based on data基于数据处理调用
【发布时间】:2020-04-09 12:45:40
【问题描述】:

我正在尝试使用单个端点构建 API (.NET Core 3.1)。应如何处理此调用取决于发送它的数据。

我发现了多态性和自定义数据绑定的示例。但感觉不适合这个例子,因为我想根据给定的 Type 属性处理所有内容。

[HttpPost]
    public IActionResult CreatePayment([FromBody]PaymentRequest request)
    {
        if (request.Type == "MultiSafepay")
        {
            // cast and do specific logic
        }
        else if(request.Type == "Other")
        {
            // cast and do specific logic
        }
        return Ok();
    }

public class MultiSafepayPaymentResponse : PaymentResponse
{
    public string PaymentUrl { get; set; }
    public string QRCodeUrl { get; set; }
}

public class PaymentRequest
{
    public string Type { get; set; }
    public string Amount { get; set; }
    public string Description { get; set; }
}

这几天我都在为此头疼。希望大家能帮帮我。

提前致谢!

【问题讨论】:

标签: c# api asp.net-core .net-core endpoint


【解决方案1】:

我有点傻。使用原始请求并手动转换更为实用。 但是,如果不可能或不希望您使用自定义模型绑定。绑定起来有点棘手custom model binding documentation

编辑:

Martin 指出了一个 SO 问题,将解释消耗原始 json link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多