【发布时间】:2019-03-16 02:25:01
【问题描述】:
这就是我目前尝试使用 REST API 的方式
private HttpClient client = new HttpClient();
public HomeController(IAuthenticationClient authenticationClient)
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress = new Uri("http://localhost:55260/api/Accounts/");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
this.authenticationClient = authenticationClient;
}
public IActionResult Index()
{
var result = GetList();
return View();
}
public async Task<List<string>> GetList()
{
HttpResponseMessage response = await client.GetAsync("GetList");
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsAsync<List<string>>();
}
return new List<string>();
}
这是被调用的 API:
[HttpGet]
[Route("GetList")]
[AllowAnonymous]
public ActionResult<IEnumerable<string>> GetList()
{
return new string[] { "value1", "value2" };
}
问题是,如果我在 Postman 上测试它,我会得到正确的结果,即 value1 和 value2,但是,当我从我的 api 客户端调用它时,我会得到这个奇怪的结果而不是正确的结果。
我已经尝试了很多不同的指南,但似乎没有一个有效。是否有用于处理 .net core 2.1 的 REST api 的库?还是我最好只使用 httpclient?
【问题讨论】:
-
如果它是从邮递员那里工作的,请点击保存按钮附近的代码链接从邮递员那里获取代码
标签: c# asp.net asp.net-web-api asp.net-core asp.net-core-mvc