I need to consume a web api through an mvc project synchronously, however most of the examples are asynchronous,
I have the following code:
[HttpGet]
public async Task<ActionResult> Buscar(int? id)
{
//Get
var vm = new VM_Prov();
VM_Proveedores v = new VM_Proveedores();
if (id.HasValue)
{
string apiUrl = "http://localhost:61698/Proveedor/Buscador?id=" + id;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
v = Newtonsoft.Json.JsonConvert.DeserializeObject<VM_Proveedores>(data);
return View("Proveedores", v);
}
}
}
return View("Index", vm);
}
Is there any way to do it in a synchronic way?