简体   繁体   English

如何在 ASP.NET Core MVC 中请求和显示实时 web api 数据?

[英]How to request and display real-time web api data in ASP.NET Core MVC?

I'm using the CoinGecko api to get real-time price data on several cryptocurrencies in an ASP.NET Core MVC web application.我正在使用CoinGecko api 在 ASP.NET Core MVC web 应用程序中获取几种加密货币的实时价格数据。 CoinGecko's price data is updated every 1-10 minutes, so I want to request price data probably every 5 minutes. CoinGecko 的价格数据每 1-10 分钟更新一次,所以我想大概每 5 分钟请求一次价格数据。 I want the data to refresh on the client-side so the user doesn't have to refresh the page to see up-to-date data.我希望数据在客户端刷新,这样用户就不必刷新页面来查看最新数据。 I want that data to be displayed in a line graph chart.我希望这些数据显示在折线图中。

I want to make sure I'm thinking about this correctly so here's my current thought process:我想确保我正确地思考这个问题,所以这是我目前的思考过程:

I can use SignalR to create an open connection between the client and the server, so the client does not have to refresh to get coingecko api data.我可以使用SignalR在客户端和服务器之间创建开放连接,因此客户端无需刷新即可获取 coingecko api 数据。 I chose SignalR because it will choose the best transport technique for the situation, and I know very little about websockets, long-polling, etc.我选择 SignalR 是因为它会选择最适合这种情况的传输技术,而我对 websockets、长轮询等知之甚少。

As far as displaying that data, I've decided to use the Highcharts library, since it's easy to learn.至于显示这些数据,我决定使用Highcharts库,因为它很容易学习。

So essentially my question is, how do I get real time web api data at set intervals and display it in a chart without having to refresh the client?所以基本上我的问题是,我如何以设定的时间间隔获取实时 web api 数据并将其显示在图表中而无需刷新客户端? Is my thought process correct?我的思维过程正确吗?

Any helpful insights or links to articles would be appreciated.任何有用的见解或文章链接将不胜感激。 I'm not really sure how to proceed so I want to make sure I'm not wasting my time.我不确定如何进行,所以我想确保我没有浪费时间。

You could try to create scheduled Background tasks with hosted services .您可以尝试使用托管服务创建计划的后台任务 Then, you can also use the Cronos package and the Cron Expressions to configure the scheduled task, and set the Cron Expressions to run the task at special date range or run the Schedule Job per 5 minutes.然后,您还可以使用 Cronos package 和 Cron 表达式来配置计划任务,并设置 Cron 表达式在特定日期范围内运行任务或每 5 分钟运行一次计划作业。

More detail about creating a scheduled tasks, see How can I execute a method every year using BackgroundService in ASP.NET core Application?有关创建计划任务的更多详细信息,请参阅如何在 ASP.NET 核心应用程序中使用 BackgroundService 每年执行一个方法? . .

Then, in the task, you can call the CoinGecko api to get the latest data, then, inject an instance of IHubContext and call the client method to send the data to the client and display them.然后,在任务中,可以调用CoinGecko api获取最新数据,然后注入IHubContext实例,调用客户端方法将数据发送给客户端并显示出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ASP.NET 核心 mvc 向 ASP.NET 核心中的 Web API 发出 PUT 请求? - How to make a PUT request from ASP.NET core mvc to Web API in asp.net core? 使用 ASP.NET Core MVC 将带有文件的数据发布到 api (ASP.NET Core Web API) - Post data with files using ASP.NET Core MVC to api (ASP.NET Core Web API) 点网核心——实时web api如何处理后续请求中的并发 - Dot net core - How does real-time web api handle concurrency in subsequent requests 如何将ASP.NET MVC核心视图和ASP.NET WEB API控制器分离到单独的项目中? - How to seperate ASP.NET MVC core view and ASP.NET WEB API controllers into separate projects? 如何使 PUT 从 ASP.NET MVC 到 ASP.NET 核心 Web ZDB974238714CA8A44A7CE1D0? - How to make PUT from ASP.NET MVC to ASP.NET Core Web API? asp.net mvc -view显示实时进度状态 - asp.net mvc -view displaying real-time progress status 使用ASP.NET Core MVC控制器将RestSharp POST请求连接到Web API - issue connecting RestSharp POST request to web API with ASP.NET Core MVC controller ASP.NET C#-实时通知方法 - ASP.NET C# - A Real-Time Notifications Method ASP.NET MVC核心Web POST请求使用NULL值填充数据模型 - ASP.NET MVC Core Web POST Request populating Data Model with NULL values 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM