Wednesday 10 June 2015

how to call web api in mvc controller for post method


//[HttpPost]
   public async Task<ActionResult> AddEmp(MyObj myObj)
{
  try
{
  var apiResult= await SendToApi(HttpVerbs.Post, "Employee/AddEmp", myObt);
if (apiResult.IsSuccessStatusCode)
{
  try
{
  return new HttpStatusCodeResult(HttpStatusCode.OK);
}
  catch (Exception ex)
{
  Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(ex.BrokenRules);
}
  return new HttpStatusCodeResult(HttpStatusCode.OK);
}
  // If Failed return status code and the Broken rules
Response.StatusCode = (int)apiResult.StatusCode;
return Json(apiResult.Content.ReadAsAsync<List<CustomRule>>().Result);
}
  catch (Exception ex)
{
  Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(ex.CustomRule);
}

No comments:

Post a Comment