2016-05-15 244 views
9

我們希望能夠使用ASP.NET Core MVC Web應用程序中的包Microsoft.AspNet.WebApi.Client對外部系統進行HTTP調用。它確實工作,但我無法在.NET核心(github)中找到相應的源代碼。從ASP.NET路線圖的角度來使用這個庫可以嗎? ASP.NET Core會在未來支持嗎?最重要的是,作爲ASP.NET Core/.NET Core的一部分,這個軟件包是否會在非Windows平臺上得到支持?我們可以使用ASP.NET Core應用程序中的Microsoft.AspNet.WebApi.Client嗎?

+0

這是一個很好的問題。我也會對答案非常感興趣。 –

回答

5

您可以嘗試我爲REST客戶端所做的操作。我發現你在最新版本中提到的程序集在最近發佈的ASP.Net Core 1.0中不起作用。而不是"Microsoft.AspNet.WebApi.Client",請使用"System.Net.Http"

然後,你就已經建立了這樣一個HTTP POST請求:

using AvailabilityPricingClient.Core; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using AvailabilityPricingClient.Core.Model; 
using System.Net.Http; 
using System.Net.Http.Headers; 

namespace AvailabilityPricingClient.Client 
{ 
    public class ProductAvailabilityPricing : IProductAvailabilityPricing 
    { 
     private HttpClient _client; 
     public ProductAvailabilityPricing(string apiUrl) 
     { 
      _client = new HttpClient(); 
      _client.BaseAddress = new Uri(apiUrl); 
      _client.DefaultRequestHeaders.Accept.Clear(); 
      _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     } 

     public void Dispose() 
     { 
      _client.Dispose(); 
     } 


     public async Task<IEnumerable<Availablity>> GetAvailabilityBySkuList(IEnumerable<string> skuList) 
     { 
      HttpResponseMessage response = _client.PostAsJsonAsync("/api/availabilityBySkuList", skuList).Result; 

      if (response.IsSuccessStatusCode) 
      { 
       var avail = await response.Content.ReadAsAsync<IEnumerable<Availablity>>(); 
       return avail; 
      } 

      return null; 
     } 
    } 
} 

現在,您將建立這樣的:

using AvailabilityPricingClient.Core; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using AvailabilityPricingClient.Core.Model; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using Newtonsoft.Json; 

namespace AvailabilityPricingClient.Client 
{ 
    public class ProductAvailabilityPricing : IProductAvailabilityPricing 
    { 
     private HttpClient _client; 
     public ProductAvailabilityPricing(string apiUrl) 
     { 
      _client = new HttpClient(); 
      _client.BaseAddress = new Uri(apiUrl); 
      _client.DefaultRequestHeaders.Accept.Clear(); 
      _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     } 

     public void Dispose() 
     { 
      _client.Dispose(); 
     } 

     public async Task<IEnumerable<Availablity>> GetAvailabilityBySkuList(IEnumerable<string> skuList) 
     { 
      var output = JsonConvert.SerializeObject(skuList); 
      HttpContent contentPost = new StringContent(output, System.Text.Encoding.UTF8, "application/json"); 
      HttpResponseMessage response = _client.PostAsync("/api/availabilityBySkuList", contentPost).Result; 

      if (response.IsSuccessStatusCode) 
      { 
       var avail = await response.Content.ReadAsStringAsync() 
        .ContinueWith<IEnumerable<Availablity>>(postTask => 
        { 
         return JsonConvert.DeserializeObject<IEnumerable<Availablity>>(postTask.Result); 
        }); 
       return avail; 
      } 

      return null; 
     } 
    } 
} 

這樣你的界面不會改變只是身體的請求代碼更改。

這對我有用....祝你好運....

相關問題