This is my actual wrapper code :
namespace RestApiTO.Wrapper
{
public class RequestModel<T> : AttributeModel<T>
{
public RequestModel(T data)
{
Data = data;
}
public T Data { get; set; }
}
public class AttributeModel<T>
{
public AttributeModel()
{
}
public AttributeModel(T data)
{
Attributes = data;
}
public T Attributes { get; set; }
}
public class OrderRequestModel<T> : AttributeModel<T>
{
public OrderRequestModel(T attributes, List<Order_items> dataOrderItems)
{
this.Attributes = attributes;
this.Order_detail = dataOrderItems;
}
public int CustomerId { get; set; }
public List<Order_items> Order_detail { get; set; }
}
}
This is my expected request body :
{
"data": {
"attributes": {
"user_id": 1,
"order_detail": [
{
"product_id": 1,
"quantity": 1
},
{
"product_id": 2,
"quantity": 2
}
]
}
}
}
This is schema database :
Schema database
This is my controller
[HttpPost]
public async Task<ActionResult> PostOrders([FromBody]RequestModel<AttributeModel<OrderRequestModel<Order_items>>> orders)
{
// do something }
If I’m using this code there is getting error:
Each parameter in constructor ‘Void .ctor(System.Guid, System.String, System.String, System.String, System.String)’ on type ‘xxxxxxx’ must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.
I want to make my request body as my expected above, but it’s more difficult that of my mind.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You could add parameterless constructor to your classes. Than during serialization it creates object using this constructor. After that it goes and map request properties to your object properties using setter.
Also here a good explanation how deserealization works and how it is looking for constructors.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0