refresh every 5 minutes en asp.net mvc

2019-09-10 11:19发布

The controller display the data from the excel sheet. I need that the controller check the excel sheet every 1 hour, also the views should be updated. This is my controller code:

string path3 = "D:/Project/Sesame Incident Dump_20160317.xls";

Excel.Application application3 = new Excel.Application();
Excel.Workbook workbook3 = application3.Workbooks.Open(path3);
Excel.Worksheet worksheet3 = workbook3.ActiveSheet;
Excel.Range range3 = worksheet3.UsedRange;
List<SesameIncident> ListSesameIncident = new List<SesameIncident>();
for (int row = 2; row <= range3.Rows.Count; row++)
{
    SesameIncident S = new SesameIncident();
    S.Number = (((Excel.Range)range3.Cells[row, 1]).Text);
    S.AssignedTo = (((Excel.Range)range3.Cells[row, 5]).Text);
    S.Opened = (((Excel.Range)range3.Cells[row, 6]).Text);
    S.Status = (((Excel.Range)range3.Cells[row, 7]).Text);
    S.Priority = (((Excel.Range)range3.Cells[row, 10]).Text);
    S.AssignedGroup = (((Excel.Range)range3.Cells[row, 12]).Text);

    ListSesameIncident.Add(S);

}

ViewBag.ListSesameIncidents = ListSesameIncident
       .Where(x => x.Status == "Pending Customer").Take(13);

4条回答
劫难
2楼-- · 2019-09-10 12:02
<script type="text/javascript">
    setTimeout(function () { 
      location.reload();
    }, 5 * 60 * 1000);
</script>

refer Refresh Page for interval using js

You can refresh the page this way

for the controller you might need a table in database to refer, when was last updated, for reference you will have to store reference data permanently , this is my opinion, I never had such requirement

查看更多
相关推荐>>
3楼-- · 2019-09-10 12:03

You can add a Header to your HttpContext.Response in your controller

HttpContext.Response.Headers.Add("refresh", "300; url=" + Url.Action("Index"));
查看更多
虎瘦雄心在
4楼-- · 2019-09-10 12:04

To run something periodically without user interaction (that is, without a request to initiate it), a web application isn't what you want. Instead, you're looking for either a Windows Service or perhaps a simple Console Application scheduled to run at regular intervals by the host system's scheduling software (Windows Task Scheduler, cron, etc.). See How to execute a method in Asp.net MVC for every 24 hours

I would rather think about caching that could potentially save reading xls every time. See How to cache data in a MVC application

查看更多
Juvenile、少年°
5楼-- · 2019-09-10 12:08
  • To update the client every X second is quite simple. Just use a meta http-equiv With the value refresh in you page's Header.

    This solution is clean and easy to read and you will not be depending of a simple JavaScript loop.

  • To update your excel sheet every X, you need another app with a Timer. You can do whatever you want, if you're using .net, a simple console application will do the work. If you are using Azure you could just use a worker role, that is exactly what a worker Is about ;p

查看更多
登录 后发表回答