.NET Core 2.1 HttpClient no devuelve los valores esperados
Estoy usando una interfaz para llamar a una API desde esta urlhttp: // localhost: 55260 / api / Accounts / GetList
Este es el controlador al que hace referencia:
[HttpGet]
[Route("GetList")]
[AllowAnonymous]
public ActionResult<IEnumerable<string>> GetList()
{
return new string[] { "value1", "value2" };
}
Sin embargo, en lugar de que vuelvan las cadenas, obtengo esto:
Así es como estoy declarando mi httpclient / interfaz:
private readonly HttpClient httpClient;
public AuthenticationClient(HttpClient httpClient)
{
httpClient.BaseAddress = new Uri("http://localhost:55260/api/Accounts");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
this.httpClient = httpClient;
}
public async Task<IEnumerable<string>> GetDataAsync()
{
List<string> result = null;
HttpResponseMessage response = await httpClient.GetAsync("/GetList");
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<List<string>>();
}
return result;
}
Ya lo he declarado en mi Startup.cs como services.AddHttpClient ();
Así es como llamo a la interfaz
private readonly IAuthenticationClient authenticationClient;
public HomeController(IAuthenticationClient authenticationClient)
{
this.authenticationClient = authenticationClient;
}
public IActionResult Index()
{
var result = authenticationClient.GetData();
return View();
}
¿Me perdí algo o hay un tutorial sobre cómo usar HttpClients? Además, ¿cómo publico datos a través de esto?