我正在嘗試從 api repos 端點獲取 repos 串列,但是我在方法中實作它時遇到問題。我得到的錯誤表明它無法將 IEnumerable RepoData 轉換為 IEnumerable RepoData。這是我到目前為止所擁有的:
public class RepoResult
{
public IEnumerable<RepoData> RepoData { get; set; }
}
public class RepoData
{
public int Id { get; set; }
public string Name { get; set; }
public string Html_Url { get; set; }
}
public async Task<IEnumerabe<RepoData>> LoadRepos()
{
string url = "https://api.github.com/users/jeremyolu/repos";
using (HttpResponseMessage response = await _apiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
RepoResult result = await response.Content.ReadAsAsync<RepoResult>();
return result.RepoData;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
uj5u.com熱心網友回復:
無需定義 RepoResult 類,只需執行以下操作:
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<IEnumerable<RepoData>>(await response.Content.ReadAsStringAsync());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/443826.html
標籤:C# 。网 asp.net-mvc httpclient
上一篇:IF陳述句不一致
下一篇:<label>元素拆分為多行
