asp.net-mvcmodel-view-controllerasp.net-mvc-viewmodel

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


I am fetching data from API and trying to convert it into deserialized form. But gettting this error.

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);

        }

Solution

  • Use await before response

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