Our Project architecture is AngularJS + MVC + WebAPI. Angular JS would call MVC action and MVC action further calls API methods. Below are some of the action methods we used for calling api methods. Note that our project is in its final stage, and as such we cannot modify the existing architecture. We have to use AngularJS + MVC + WebAPI.
We are having lot of controllers and each controller contains lot of get/post methods. I need suggestion about below part of code. because similar to below piece of code only we are using across all the controllers with different method names. Is there any other better way/ Generic way to call webapi from MVC.
public async Task<ActionResult> GetStates()
{
httpClient = HttpContext.Items["httpclient"] as HttpClient;
HttpResponseMessage response = await httpClient.GetAsync(httpClient.BaseAddress + "api/Admin/GetStates");
if (response.IsSuccessStatusCode)
{
string stateInfo = response.Content.ReadAsStringAsync().Result;
if (!string.IsNullOrEmpty(stateInfo))
{
return new ContentResult { Content = stateInfo, ContentType = "application/json" };
}
}
return new JsonResult();
}
public async Task<ActionResult> ClientsInfo()
{
httpClient = HttpContext.Items["httpclient"] as HttpClient;
HttpResponseMessage response = await httpClient.GetAsync(httpClient.BaseAddress + "api/Admin/GetClients");
if (response.IsSuccessStatusCode)
{
string roleInfo = response.Content.ReadAsStringAsync().Result;
if (!string.IsNullOrEmpty(roleInfo))
{
return new ContentResult { Content = roleInfo, ContentType = "application/json" };
}
}
return new JsonResult();
}
public async Task<ActionResult> GetRoles()
{
httpClient = HttpContext.Items["httpclient"] as HttpClient;
HttpResponseMessage response = await httpClient.GetAsync(httpClient.BaseAddress + "api/Roles");
if (response.IsSuccessStatusCode)
{
string roleInfo = response.Content.ReadAsStringAsync().Result;
if (!string.IsNullOrEmpty(roleInfo))
{
return new ContentResult { Content = roleInfo, ContentType = "application/json" };
}
}
return new JsonResult();
}
...........
//for post and delete actions similarly