2014-03-03 74 views
15

如何使控制器和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> 

回答

17

你可以從你的ASP.NET MVC控制器內發送一個HTTP請求到Web API控制器:

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); 
    } 
} 

此外,如果你能充分利用.NET 4.5異步/ AWAIT強烈建議這樣做,以避免阻塞調用:

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); 
    } 
} 
+0

我創建了一個HTML客戶端調用API使用URL/API /它的工作產品,但沒有與問題(「http://yourapi.com/api/products 「)我應該把什麼放在這裏? – user3353484

+0

您應該將Web API端點的URL返回到產品列表。 –

+0

謝謝!我可以通過@ Url.Action替換網址嗎? – user3353484

12

閱讀here如何使用Razor視圖引擎的Web API控制器內。有趣的部分是使用RazorEngine NuGet package來完成繁重的工作。

+0

感謝您的有用鏈接。 –

+0

你的答案肯定比從MVC到API控制器的HTTP請求更好。我不知道爲什麼答案有這麼多upvotes,但從性能角度來看,這是無稽之談。 – Regfor

0

twoflower提供的鏈接的基礎是讓您的處理程序創建並返回自定義IHttpActionResult實現的實例。一個簡單的例子如下所示:

public class TestHttpActionResult : IHttpActionResult 
{ 
    private readonly HttpRequestMessage _request; 
    private readonly string _responseString; 

    public TestHttpActionResult(HttpRequestMessage request, string responseString) 
    { 
     _request = request; 
     _responseString = responseString; 
    } 

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
    { 
     var response = _request.CreateResponse(HttpStatusCode.Created); 
     response.Content = new StringContent(_responseString); 
     response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); 
     return Task.FromResult(response); 
    } 
}