问题
I am currently trying to accept a POST
request to a .NET Core Web API endpoint that accepts the following model:
public class OrderPost
{
[Required]
public string DeliveryStreet { get; set; }
public string DeliveryBuilding { get; set; }
[Required]
public string DeliveryCity { get; set; }
[Required]
public string DeliveryProvince { get; set; }
[Required]
public string DeliveryPostalCode { get; set; }
[Required]
public string CardName { get; set; }
[Required]
public string CardNumber { get; set; }
[Required]
public string CardExpiry { get; set; }
[Required]
public long CardCvv { get; set; }
[Required]
public List<OrderItem> OrderItems { get; set; }
}
public class OrderItem
{
public long Id { get; set; }
public string Category { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Uri ImageUrl { get; set; }
public long Price { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
public long Quantity { get; set; }
}
When I remove the List<OrderItem>
and post the data without the List the call works perfectly. As soon as I add the list back and try and POST
the data I get a 400 Bad Request
error. I have a feeling I am just missing some attribute or something on the List. The JSON I am posting is valid, I double checked that. Any ideas of what could be causing this?
Here is the JSON I am POST'ing:
{
"deliveryStreet": "Test street",
"deliveryBuilding": "",
"deliveryCity": "TEst",
"deliveryProvince": "TEst",
"deliveryPostalCode": "0852",
"cardName": "Jane Jones",
"cardNumber": "4711 1000 0000 0000",
"cardExpiry": "05 / 20",
"cardCvv": "123",
"orderItems": [{
"id": 1,
"category": null,
"name": "Test",
"description": "Test test test",
"imageUrl": "http://google.com/",
"price": 625,
"createdAt": "2019-02-21T13:25:56.709192",
"updatedAt": "2019-02-21T13:25:56.709192",
"quantity": 5
}]
}
Here is the Endpoint that I am trying to POST
to:
[HttpPost]
public IActionResult Post([FromBody]OrderPost data)
{
//Get the credit card type off of the number
var type = StringUtilities.GetCardTypeFromNumber(data.CardNumber);
//Check if valid type was found
if (type == "UNKNOWN")
return BadRequest("Unknown credit card type");
float total = 0;
.................
}
回答1:
How about you send ?
data: {
deliveryObj: yourDeilveryObj,
orderItems: yourOrderItemsArray
}
And your json
obj should look like the following:
{
"deliveryObj":
{
"deliveryStreet": "Test street",
"deliveryBuilding": "",
"deliveryCity": "TEst",
"deliveryProvince": "TEst",
"deliveryPostalCode": "0852",
"cardName": "Jane Jones",
"cardNumber": "4711 1000 0000 0000",
"cardExpiry": "05 / 20",
"cardCvv": "123",
},
"orderItems": [{
"id": 1,
"category": null,
"name": "Test",
"description": "Test test test",
"imageUrl": "http://google.com/",
"price": 625,
"createdAt": "2019-02-21T13:25:56.709192",
"updatedAt": "2019-02-21T13:25:56.709192",
"quantity": 5
}]
}
Now, at the backend, you should receive it as
JObject
.
Your receiving method should look like thisPost(JObject objData)
. Now you can break down this objData
to JObject
and JArray
.
It would look like something:
[HttpPost]
public IHttpActionResult POST(JObject objData)
{
dynamic jsonData = objData;
//DeliveryObj
JObject deliveryObjJson = jsonData.deliveryObj;
var deliveryObj= deliveryObjJson .ToObject<OrderPost>();
//OrderItemsArray
JArray orderItemsJson = jsonData.orderItems;
var orderItems = orderItemsJson.Select(item => item.ToObject<OrderItem>()).ToList();
return Ok(_services.Post(deliveryObj, orderItems ).Data);
}
回答2:
For Asp.Net Core Web api, Model validation errors automatically trigger an HTTP 400 response, check Automatic HTTP 400 responses .
Try to make a test with code below:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
来源:https://stackoverflow.com/questions/54809578/nested-list-json-net