Wednesday 10 June 2015

how to call web api in mvc controller get method


[HttpGet]
public async Task<ActionResult> GetAllEmp(atring type)
{
  if (!string.IsNullOrEmpty(type?.ToString()))
{
  var apiResult =await SendToApi(HttpVerbs.Get,string.Format("Employee/GetAllEmp/{0}", type), ));
if (apiResult .IsSuccessStatusCode)
{
  return Json(apiResult.Content.ReadAsAsync<List<MyEmpObjModel>>().Result,JsonRequestBehavior.AllowGet);
}
  // If Failed return status code and the Broken rules
Response.StatusCode = (int)(apiResult .StatusCode;
return Json((apiResult .Content.ReadAsAsync<List<CustomRule>>().Result);
}
  Response.StatusCode = (int) HttpStatusCode.BadRequest;
return Json(new CustomRule("", "Etype required"));
}

No comments:

Post a Comment