Wednesday, 5 April 2017

How to call WEB API in ASP.NET MVC ?


ASP.NET WEB API can be called by using HttpClient and WEB API address as given below:

public class ProductController : Controller
 {
 HttpClient Client = new HttpClient();
Uri BaseAddress = new Uri("http://localhost:131/");
 public ActionResult Index()
 {
Client.BaseAddress = BaseAddress; HttpResponseMessage response = Client.GetAsync("productservice/GetProducts").Result;
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsAsync<IEnumerable<Product>>().Result; return View(data);
}
return View();
 }
}

No comments:

Post a Comment