如何在C#的WebAPI后端接收JSON数据?
我从JavaScript前端发送了以下JSON数据。
{
"User_Id": 1,
"TotalPrice": 35,
"DeliveryAddress": "At my house",
"CartItems": [
{
"Id": 1009,
"Name": "Superman juni 2014",
"Quantity": 1,
"Price": 35
}
]
}
我有这些类:
public class PurchaseOrder
{
public List<CartItem> CartItems { get; set; }
public string DeliveryAddress { get; set; }
public int TotalPrice { get; set; }
public int User_Id { get; set; }
}
public class CartItem
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public int Price { get; set; }
}
我的WebAPI方法:
[System.Web.Mvc.HttpPost]
public bool AddOrder(PurchaseOrder order)
{
// Here I will do something
return true;
}
当我使用“PurchaseOrder order”对象时,结果只会得到“null”。问题可能是我使用了[System.Web.Mvc.HttpPost]吗?我也尝试过[System.Web.Http.HttpPost],但结果相同。 // Martin
application/json
吗? - Markusorder
的JavaScript对象,并在传输时使用JSON.stringify
进行格式化。 - Amit Kumar GhoshSystem.Web.Mvc.HttpPost
,它应该来自Http
命名空间,我认为不是Mvc
。其次,这与您的JavaScript有关。 - Callum Linington