使用Web API返回的Razor视图ASP.NEt MVC [英] ASP.NEt MVC using Web API to return a Razor view
问题描述
如何使控制器返回和生成的剃刀了解从API我想保持剃刀引擎视图中的数据的查看和使用API
原来的MVC控制器返回的数据作为参数,现在我想的看法从API数据
MVC控制器
公共类ProductController的:控制器
{
公众的ActionResult指数()
{
返回查看();
}
API控制器
公共ProductsController类:ApiController
{
私人ApplicationDbContext DB =新ApplicationDbContext(); //获取API /产品
公共IEnumerable的<产品与GT;的GetProducts()
{
返回db.Products;
}
}
型号:
@model IEnumerable的< WebApplication2.Models.Product>@ {
ViewBag.Title =指数;
}< H2>指数< / H>&所述p为H.;
@ Html.ActionLink(新建,创建)
&所述; / P>
<表类=表>
&所述; TR>
<第i个
@ Html.DisplayNameFor(型号=> model.Name)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Category)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Price)
< /第i
百分位>< /第i
< / TR>@foreach(以型号VAR项){
&所述; TR>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Name)
< / TD>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Category)
< / TD>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Price)
< / TD>
&所述; TD>
@ Html.ActionLink(编辑,编辑,新{ID = item.Id})|
@ Html.ActionLink(详细信息,详细信息,新{ID = item.Id})|
@ Html.ActionLink(删除,删除,新{ID = item.Id})
< / TD>
< / TR>
}
< /表>
您可以从ASP.NET MVC控制器内发送一个HTTP请求到Web API控制器:
公共类ProductController的:控制器
{
公众的ActionResult指数()
{
VAR的客户=新的HttpClient();
VAR响应= client.GetAsync(http://yourapi.com/api/products)。结果;
变种产品= response.Content.ReadAsAsync<&IEnumerable的LT;产品与GT;>()结果。
返回查看(产品);
}
}
此外,如果你可以利用.NET 4.5异步电动机/等待着强烈建议这样做,以避免阻塞调用:
公共类ProductController的:控制器
{
公共异步任务<&的ActionResult GT;指数()
{
VAR的客户=新的HttpClient();
变种响应=等待client.GetAsync(http://yourapi.com/api/products);
变种产品=等待response.Content.ReadAsAsync<&IEnumerable的LT;产品与GT;>();
返回查看(产品);
}
}
How to make the View returned by the controller and generated by Razor get the data from the api i want to keep the razor engine view and use the api the original mvc controller returns the view with the data as parameter now i want the data from the api
MVC controller
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
Api Controller
public class ProductsController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/Products
public IEnumerable<Product> GetProducts()
{
return db.Products;
}
}
Model:
@model IEnumerable<WebApplication2.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Category)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
You could send an HTTP request to your Web API controller from within the ASP.NET MVC controller:
public class ProductController : Controller
{
public ActionResult Index()
{
var client = new HttpClient();
var response = client.GetAsync("http://yourapi.com/api/products").Result;
var products = response.Content.ReadAsAsync<IEnumerable<Product>>().Result;
return View(products);
}
}
Also if you can take advantage of the .NET 4.5 async/await it is strongly recommended to do so to avoid blocking calls:
public class ProductController : Controller
{
public async Task<ActionResult> Index()
{
var client = new HttpClient();
var response = await client.GetAsync("http://yourapi.com/api/products");
var products = await response.Content.ReadAsAsync<IEnumerable<Product>>();
return View(products);
}
}
这篇关于使用Web API返回的Razor视图ASP.NEt MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!