我正在尝试使用单个端点构建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; }
}
几天来,我都为此感到沮丧。希望大家能帮助我。
提前感谢!
我有点愚蠢。使用原始请求并手动强制转换更为实用。但是,如果无法实现或希望使用自定义模型绑定。通过绑定,它会有些棘手custom model binding documentation
编辑:
Martin提出了一个SO问题,该问题将解释为使用原始json link