簡體   English   中英

從 ASP.NET 核心獲取數據 API

[英]Getting data from ASP.NET Core API

我正在開發市場和金融新聞應用程序。我從https://www.marketaux.com/獲取了 API。 我試圖將網站上的新聞顯示到我的主頁上。 我在 ASP.NET 核心 web 應用程序文件中創建了一個 model 文件和 controller。 在 controller 文件中,我在response.data部分收到錯誤,因為它顯示的響應沒有數據 object 而當我打印response.Content的 output 時,它有數據 object。你能告訴我如何解決這個問題並獲得訪問權限嗎到 API 中的數據,以便我可以將其顯示在我的主頁上?

Controller class:

using System.Runtime.CompilerServices;
using Azure.Core;
using MarketNews.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;
using Newtonsoft.Json;
using RestSharp;

namespace MarketNews.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class NewsController : ControllerBase
    {
        [HttpGet]
        public News[] GetNews() 
        {
            List<News> news = new List<News>();
            RestClient client = new RestClient("https://api.marketaux.com/v1/news/all");
            // client.Timeout = -1;

            RestRequest request = new RestRequest("News",Method.Get);
            request.AddQueryParameter("api_token", "qIWtsblpK93oeo23o87egUGBoVmVaqkl4fdHRTEc");
            request.AddQueryParameter("symbols", "aapl,amzn");
            request.AddQueryParameter("limit", "50");

            RestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

            if (response != null)
            {
                foreach (var article in response.data)
                {
                    news.Add(new News
                    {
                        Title = article.title,
                        Description = article.description,
                        Url = article.url,
                        Url_Image = article.image_url,
                        Published_At = article.published_at
                    });
                }
            }

            return news.ToArray();
        }
    }
}

Model class

namespace MarketNews.Models
{
    public class News
    {
        public string Title;
        public string Description;
        public string Url;
        public string Url_Image;
        public DateTime Published_At;
    }
}

我想從 API 獲取數據並將其顯示在我的主頁上。 我使用RestClientRestRequest來獲取響應,然后從響應中獲取響應。 response.Content打印到控制台時,我得到了 output,它正在工作,它給了我一個 json 文件作為 output。在 foreach 循環中,當我嘗試將響應數據設置為我創建的 model 數據時,它顯示response.data沒有'不存在。

我想知道這里出了什么問題,或者有沒有其他方法可以從 API 獲取數據?

Api:網頁鏈接

RestSharp 文檔中, Execute()方法確實支持泛型類型。

Execute<T>(IRestRequest request)

請注意,此方法已棄用。 您應該尋找以下方法:

ExecuteAsync<T>(IRestRequest request, CancellationToken cancellationToken)

並修改您的 API 操作以支持異步操作。

從鏈接中共享的響應數據中,您需要一個Root object,其中包含List<New>類型的data屬性。

  1. 為響應定義數據 model class。
using System.Text.Json;

public class Root
{
    [JsonPropertyName("data")]
    public List<News> Data { get; set; }
}
public class News
{
    [JsonPropertyName("title")]
    public string Title { get; set; }
    
    [JsonPropertyName("description")]
    public string Description { get; set; }

    [JsonPropertyName("url")]
    public string Url { get; set; }

    [JsonPropertyName("image_url")]
    public string Url_Image { get; set; }

    [JsonPropertyName("published_at")]
    public DateTime Published_At { get; set; }
}
  1. 對於調用方,將泛型類型指定為Root 接下來使用response.Data.Data從響應中提取data屬性。
RestResponse<Root> response = client.Execute<Root>(request);

// asynchronous way
// RestResponse<Root> response = await client.ExecuteAsync<Root>(request);

if (response != null)
    news = response.Data.Data;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM