I have a model and controller like the following
public class OrderModel
{
[JsonProperty("order_id")]
public string ID { get; set; }
[JsonProperty("delivery_shift")]
public string DeliveryShift { get; set; }
public string InvoiceType { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase
{
[HttpPost]
public IActionResult Index([FromBody] OrderModel order) {
return new JsonResult(new
{
order.ID,
order.DeliveryShift,
order.InvoiceType
});
}
}
Now when i submit a post request with this json in the body then order_id
and delivery_shift
doesn't map with the model.
{"order_id": "OrderID", "delivery_shift": "evening", "InvoiceType": "DUE"}
but it works when i submit the json in this format, BUT I need the previous format to work.
{"ID": "OrderID", "DeliveryShift": "evening", "InvoiceType": "DUE"}
Thanks.
SOLVED
Thanks to @AhmetUrun Updating model with JsonPropertyName
attribute worked.
public class OrderModel
{
[JsonPropertyName("order_id")]
public string ID { get; set; }
[JsonPropertyName("delivery_shift")]
public string DeliveryShift { get; set; }
public string InvoiceType { get; set; }
}