我当前正在尝试接受POST
请求对.net Core Web API端点接受以下模型:
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; }
}
当我删除List<OrderItem>
并在没有列表的情况下发布数据时,呼叫完美效果。一旦我添加列表并尝试和POST
数据,我会遇到400 Bad Request
错误。我有一种感觉,我只是缺少列表中的一些属性或其他属性。我发布的JSON有效,我对此进行了仔细检查。关于可能导致这一点的任何想法?
这是json我是帖子:
{
"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
}]
}
这是我试图 POST
到:
[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;
.................
}
您如何发送?
data: {
deliveryObj: yourDeilveryObj,
orderItems: yourOrderItemsArray
}
和您的json
OBJ应该看起来如下:
{
"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
}]
}
现在,在后端,您应该收到它 JObject
。
您的接收方法应该看起来像Post(JObject objData)
。现在,您可以将此objData
分解为JObject
和JArray
。看起来像是什么:
[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);
}
对于ASP.NET Core Web API,模型验证错误会自动触发HTTP 400响应,检查自动HTTP 400响应。
尝试使用以下代码进行测试:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
});