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

Asked

Viewed 1,431 times

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 the same way?

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 I would send an object and return data?

1 answer

1

Try something like that:

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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.