如何使控制器和Razor生成的視圖從api獲取數據我想保留剃鬚刀引擎視圖並使用api 最初的mvc控制器返回與所述數據的視圖參數現在我想從APIASP.NEt使用Web API返回剃刀視圖的MVC
MVC控制器
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
阿比控制器
public class ProductsController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/Products
public IEnumerable<Product> GetProducts()
{
return db.Products;
}
}
沫的數據德爾:
@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>
我創建了一個HTML客戶端調用API使用URL/API /它的工作產品,但沒有與問題(「http://yourapi.com/api/products 「)我應該把什麼放在這裏? – user3353484
您應該將Web API端點的URL返回到產品列表。 –
謝謝!我可以通過@ Url.Action替換網址嗎? – user3353484