7

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; }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Mahabubul Hasan
  • 1,396
  • 13
  • 20
  • seems like duplicate of https://stackoverflow.com/questions/22492821/webapi-deserializing-and-serializing-alternate-property-names. so addindg this attribute should work `[DataMember(Name = "field_name")]` – Ahmet Urun Dec 21 '19 at 11:51
  • Does this answer your question? [WebApi - Deserializing and serializing alternate property names](https://stackoverflow.com/questions/22492821/webapi-deserializing-and-serializing-alternate-property-names) – Ahmet Urun Dec 21 '19 at 11:53
  • that solution didn't work! – Mahabubul Hasan Dec 21 '19 at 11:54
  • what is the content-type?So when you post the request check the received request's content type.It should be application/json then only it will work. – Hameed Syed Dec 21 '19 at 11:54
  • can you share the model after you've added DataMember attribute as I've told. you need add DataContract atrribute on top of your model class as well. – Ahmet Urun Dec 21 '19 at 11:57
  • i'm submitting a json using insomnia – Mahabubul Hasan Dec 21 '19 at 11:58
  • Yes i tried adding DataContract it didn't work ```C# [DataContract] public class OrderModel { [DataMember(Name = "order_id")] public string ID { get; set; } [DataMember(Name = "delivery_shift")] public string DeliveryShift { get; set; } public string InvoiceType { get; set; } } ``` – Mahabubul Hasan Dec 21 '19 at 11:59
  • BTW I'm using asp.net core 3.1 – Mahabubul Hasan Dec 21 '19 at 12:00
  • DataMember is very speciifc for WCF but for webapi JSONProperty should work.Anyway JSONProperty as precedence over DataMember – Hameed Syed Dec 21 '19 at 12:00
  • @HameedSyed no it's not specific to wcf, https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization @MahabubulHasan you need to configure web api to use DataContract Json Serializer if you haven't done already `var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true;` – Ahmet Urun Dec 21 '19 at 12:03
  • Do i need to add anything in my `StartUp.cs` file after `services.AddControllersWithViews();` this line? – Mahabubul Hasan Dec 21 '19 at 12:05
  • @AhmetUrun where do i put that line? – Mahabubul Hasan Dec 21 '19 at 12:07
  • check this question to understand usage of serializes in web api https://stackoverflow.com/questions/20371040/am-i-using-datacontractjsonserializer-or-the-json-net-one-in-my-web-api-2 – Ahmet Urun Dec 21 '19 at 12:07
  • @AhmetUrun that solution is not for asp.net core but asp.net https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization – Mahabubul Hasan Dec 21 '19 at 12:11
  • 4
    try this `[JsonPropertyName("property_name")]` – Ahmet Urun Dec 21 '19 at 12:17
  • 1
    [tag:asp.net-core-3.0] and later use a completely different JSON serializer by default. `JsonPropertyName` works with that new serializer. See: [DataMember Attribute is not honored in dotnet core 3.0](https://stackoverflow.com/q/57965350/3744182), [Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?](https://stackoverflow.com/q/55666826/3744182) and also [this answer](https://stackoverflow.com/a/58299628/3744182). – dbc Dec 21 '19 at 17:23

0 Answers0