无法从 'System.Threading.Tasks.Task<string>' 转换为 'string'

cannot convert from 'System.Threading.Tasks.Task<string>' to 'string'

我正在从 API 获取数据并尝试将其转换为反序列化形式。但是出现这个错误。

public static async Task<List<Movie>> GetAllMovies(int pageNumber, int pageSize)
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Preferences.Get("accessToken", string.Empty));
            var response = httpClient.GetStringAsync(AppSettings.ApiUrl + String.Format("api/movies/AllMovies?sort=asc&pageNumber={0}&pageSize={1}",pageNumber,pageSize));
            return JsonConvert.DeserializeObject<List<Movie>>(response);

        }

在响应前使用 await

  return JsonConvert.DeserializeObject<List<Movie>>(await response)

将您的方法更改为:

public static async Task<List<Movie>> GetAllMovies(int pageNumber, int pageSize)
{
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Preferences.Get("accessToken", string.Empty));
    var result = httpClient.GetStringAsync(AppSettings.ApiUrl + String.Format("api/movies/AllMovies?sort=asc&pageNumber={0}&pageSize={1}",pageNumber,pageSize));
    var response = await result.Content.ReadAsStringAsync();
    return JsonConvert.DeserializeObject<List<Movie>>(response);
}