Ma méthode de commande avec WebApi2Comment envoyer un objet JSON en utilisant la méthode GET
[HttpGet]
public IEnumerable<Products> GetProducts(ProductSearchCriteria searchCriteria)
{
//searchCriteria is always null here!!! Why?
return db.Instance.LoadProducts(searchCriteria);
}
Mes critères de recherche classe
public class ProductSearchCriteria
{
private int id;
private string name;
private DateTime createdOn;
[JsonProperty]
public string Name
{
get { return this.name; }
set { this.name = value; }
}
[JsonProperty]
public DateTime CreatedOn
{
get { return this.createdOn; }
set { this.createdOn = value; }
}
[JsonProperty]
public int ID
{
get { return this.id; }
set { this.id = value; }
}
}
Mon script dans la page html
<script>
$("#btnTest").on("click", function() {
var searchCriteria = {};
searchCriteria.ID = 0;
searchCriteria.Name = "";
//searchCriteria.CreatedOn = "";
var url = "http://localhost:8080/api/products"
$.getJSON(url, searchCriteria).done(processResponse);
});
function processResponse(response){
}
</script>
J'arrive ma méthode de contrôleur (mode de débogage) mais le paramètre searchSearchriteria ProductSearchCriteria est toujours null. Comment puis-je envoyer mon objet JSON en utilisant la méthode get avec JQuery et WebApi2?
Une requête GET ne doit pas avoir de corps car elle sera ignorée par le serveur, donc vous devez passer les données de la chaîne de requête en tant que paramètres simples et marquer le gestionnaire avec [FromUri] ou mieux, utilisez la méthode POST . – Gusman