0

I am trying to build an API (.NET Core 3.1) with a single endpoint. How this call should be processed depends on the data it was send with.

I found examples of polymorfism and custom data binding. But feels not right for this example because I want to process all depending of the given Type property.

[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();
    }

Classes

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; }
}

I am breaking my head on this for days. Hope you people can help me out.

Thanks in advance!

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
M1sterPl0w
  • 193
  • 3
  • 14

1 Answers1

0

I was a little bit stupid. It is more practical to use the raw request and cast manually. But if it's not possible or wanted you make use of custom model binding. With binding it's a little more tricky custom model binding documentation

Edit:

Martin poined out a SO question that will explain to consume raw json link

M1sterPl0w
  • 193
  • 3
  • 14