HOW TO MAKE A GetAsync in an ASP.NET API passing an object as a parameter?

0

I would like to know how to send an object as a parameter to an API made in ASP.NET and return data by it?

I understand that the simple method of a Get is:

HttpClient cliente = new HttpClient();

string url = "http://localhost:50501/api/values/ListaContatos";   
var response = await cliente.GetStringAsync(url);               
var contatos = JsonConvert.DeserializeObject<List<Contato>>(response);

return contatos;

... How would I do to send an object and return data?

    
asked by anonymous 03.06.2017 / 01:17

1 answer

1

Try something like this:

using (var http = new HttpClient())
{                
    var url = new Uri("http://localhost:50501/api/values/ListaContatos?id=1&nome=teste");
    var result = http.GetAsync(url).GetAwaiter().GetResult();
    var resultContent = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

    if (result.StatusCode != HttpStatusCode.OK)                {

        return null;
    }
    return JsonConvert.DeserializeObject<MyClass>(resultContent);
}
    
03.06.2017 / 04:00