I have used the correct category for my endpoint but it still shows 12 results. I have tried using postman too and same results. I want it to show a list in pagenation.
public async Task OnGetAsync(int pageNumber = 1)
{
PageNumber = pageNumber;
int pageSize = 10; // Number of articles per page
int offset = (PageNumber - 1) * pageSize;
try
{
string apiKey = "be1137cec3e34918ba30c5ce7495ea42";
string apiUrl = $"https://api.bing.microsoft.com/v7.0/news?category=US&count={pageSize}&offset={offset}&mkt=en-US";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
var response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
var newsData = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseData);
if (newsData != null && newsData.ContainsKey("value"))
{
NewsArticles = JsonConvert.DeserializeObject<List<dynamic>>(newsData["value"].ToString());
TotalResults = newsData.ContainsKey("totalEstimatedMatches") ? Convert.ToInt32(newsData["totalEstimatedMatches"]) : 0;
TotalPages = (int)Math.Ceiling((double)TotalResults / pageSize);
}
else
{
ViewData["Message"] = "No news articles found.";
}
}
else
{
string errorContent = await response.Content.ReadAsStringAsync();
_logger.LogError($"API Request failed. Status code: {response.StatusCode}, Response: {errorContent}");
ViewData["Message"] = "An error occurred while fetching news from the Bing News API.";
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while fetching news from Bing News API.");
ViewData["Message"] = "An error occurred. Please try again later.";
}