Come posso ricevere dati JSON sul mio back-end WebAPI in C#?Come ricevere i dati JSON sul backend WebAPI C#?
Ho il seguente JSON inviato dal mio frontend JavaScript.
{
"User_Id": 1,
"TotalPrice": 35,
"DeliveryAddress": "At my house",
"CartItems": [
{
"Id": 1009,
"Name": "Superman juni 2014",
"Quantity": 1,
"Price": 35
}
]
}
ho questo classi:
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; }
}
E il mio metodo WebAPI:
[System.Web.Mvc.HttpPost]
public bool AddOrder(PurchaseOrder order)
{
// Here I will do something
return true;
}
ho solo "null" come il risultato per il mio oggetto "ordine PurchaseOrder". Il problema è che sto usando [System.Web.Mvc.HttpPost]? Ho anche provato [System.Web.Http.HttpPost], ma ottengo lo stesso risultato. // Martin
Imposta Content-Type su 'application/json' sulla richiesta JavaScript? – Markus
Puoi includere la richiesta Javascript completa? – DavidG
crea un oggetto javascript denominato 'order' con i tuoi dati e usa' JSON.stringify' durante la pubblicazione. –