【发布时间】: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; }
}
这几天我都在为此头疼。希望大家能帮帮我。
提前致谢!
【问题讨论】:
-
@Martin 这也是一个解决方案:)
标签: c# api asp.net-core .net-core endpoint