I have an asp.net core MVC app that consumes an web api and displays the result in a table.
How do I refresh the table when the api has new data?
I populate the view with my controller by fetch from the api.
I have an asp.net core MVC app that consumes an web api and displays the result in a table.
How do I refresh the table when the api has new data?
I populate the view with my controller by fetch from the api.
Possible options are adding a meta tag to refresh the content at a fixed frequency, using a JavaScript timer and AJAX to refresh the content, implementing SignalR. SignalR can use a C# timer or the SQL Dependency library if the data is stored in SQL.
Thanks!
In regards to the c# timer implementation in SignalR what would that look like?
Is that added to the hub that then calls the controller to refresh?
Edit: is it like this one? https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr
In regards to the c# timer implementation in SignalR what would that look like? Is that added to the hub that then calls the controller to refresh?
The timer is on the server. Make an HTTP request to the Web API at each timer event. Just Google for code samples to find a timer programming pattern that fits your situation. It is up to you to determine if anything changed and alerting the clients. See the official SignalR docs.
if if you use signal/r, then you will write the display code in javascript. the client javascript will receive the data from the hub via a web socket, and then will update the html. you will not use razor code.
ithe question is how does your razor app know the webapi has new data? if the razor app need to poll via a timer, then the client code might as well use a timer. and you don't need signal/r.
if its just a display app, then a meta tag may be best.
Thanks all for the suggestions.
I solved this by counting the list and displaying a “refresh browser” text until the list reached my known items.
2 people are following this question.