gpt4 book ai didi

asp.net-core - 我们可以从ASP.NET Core应用程序中使用Microsoft.AspNet.WebApi.Client吗?

转载 作者:行者123 更新时间:2023-12-04 04:21:04 24 4
gpt4 key购买 nike

我们希望能够使用来自我们的ASP.NET Core MVC Web应用程序的Microsoft.AspNet.WebApi.Client包对外部系统进行HTTP调用。它确实可以工作,但是我无法在.NET Core(github)中找到相应的源代码。从ASP.NET路线图的角度来看可以使用此库吗?将来ASP.NET Core是否会支持它?最重要的是,作为ASP.NET Core/.NET Core的一部分,非Windows平台是否将支持此程序包?

最佳答案

您可以尝试我为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;
}
}
}

这样,您的界面不会只更改请求代码的主体。

这对我有用。...祝你好运....

关于asp.net-core - 我们可以从ASP.NET Core应用程序中使用Microsoft.AspNet.WebApi.Client吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com