我对 ASP.NET 和 MVC 很陌生。我已经创建了一个 MVC asp.net 应用程序,我正在寻找一种方法来显示我在视图中从任何Web API 接收到的数据,而无需预定义我的 Web API 响应的 JSON 结构。
我从 Web API 获取数据的控制器如下所示:
[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);
}
}
我的看法:
@using MVCApp.Controllers;
@{
ViewBag.Title = "Dashboard";
if (Session["userID"] == null)
{
Response.Redirect("~/Login/Index");
}
else
{
((HomeController)this.ViewContext.Controller).getCall();
}
}
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
<!-- Labels with values here! -->
</p>
</div>
<div class="col-md-4">
</div>
<div class="col-md-4">
</div>
希望有人可以帮助我。
慕妹3146593
相关分类