question

dncoso avatar image
0 Votes"
dncoso asked dncoso answered

Asp.net core mvc razor refresh UI on new web api update

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.

dotnet-aspnet-core-mvc
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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.


0 Votes 0 ·

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

0 Votes 0 ·

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.


0 Votes 0 ·
Show more comments

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.

0 Votes 0 ·

1 Answer

dncoso avatar image
0 Votes"
dncoso answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.