I'm calling a web API and the response is returned as an ContentResult.Content object. I retrieve this data in my asp.net MVC view and I'm trying to display this data in labels but I cant figure out how to parse this object into something i can work with.
Controller code:
[HttpGet]
public async Task<ActionResult> getCall()
{
string url = "http://localhost:51080/";
string customerApi = "customer/1";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(customerApi);
if (response.IsSuccessStatusCode)
{
string jsondata = await response.Content.ReadAsStringAsync();
return Content(jsondata, "application/json");
}
return Json(1, JsonRequestBehavior.AllowGet);
}
}
View:
@using MVCApp.Controllers;
@{
ViewBag.Title = "Dashboard";
if (Session["userID"] == null)
{
Response.Redirect("~/Login/Index");
}
}
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
<!-- Labels with values here! -->
</p>
</div>
<p id="rData">
</p>
<div class="col-md-4">
</div>
</div>
@section scripts {
<script>
$(document).ready(function () {
$.getJSON('/Home/getCall/', function (data) {
// parse object here
});
});
</script>
}
((HomeController)this.ViewContext.Controller).getCall();is definitely wrong.