如何重定向到 mvc 中计时器经过事件的操作结果

How to redirect to action result from timer elapsed event in mvc

我有这样的场景,我需要在计时器已用事件上重定向到新的操作结果,这是我的代码,目前无法正常工作。

   public ActionResult ExistingClientConfirmation(AppointmentRequest model, FormCollection collection)
        {
            var appointment = AppointmentRequest.GetCurrent();

          try
            {
                if (!ValidateAppointmentNotBooked(appointment))
                {
                    return indexAction(appointment);
                }
                ValidateSession(appointment);
                if (collection != null)
                {
                    model.TimeInterval = collection["hdnTimeInterval"];
                    System.Timers.Timer aTimer = new System.Timers.Timer();
                    aTimer.Elapsed += (sender, e) => OnTimedEvent(sender, e, appointment);
                    aTimer.Interval = 5000;
                    aTimer.Enabled = true;
                }
                if (model != null)
                {
                    appointment.TimeInterval = model.TimeInterval;
                    appointment.Client = model.Client;
                }
                return RedirectToAction("ExistingClientConfirmation");

            }
            catch (Exception ex)
            {
                return HandleAndRedirectException(ex);
            }
        }

 public void OnTimedEvent(object source, ElapsedEventArgs e,AppointmentRequest appointment)
        {
            HomePage(appointment);
        }

        public ActionResult HomePage(AppointmentRequest appointment)
        {
            return indexAction(appointment);
        }

在调试期间,我看到它每 5 秒触发一次 elapsed 事件,但没有重定向。有什么帮助吗?

提前致谢。

正如我在评论中所说,您不能 return 计时器事件的操作结果。客户端已经收到来自控制器的结果,并且您的计时器事件与 return 无关。所以你的计时器事件会触发,方法 Homepage() 会被执行,结果会被丢弃。

http 上下文有一个会话对象,您可以使用它来存储各种对象,例如到期日期。您可以从控制器方法访问当前会话。将日期存储在那里,在您的控制器操作中检索它并根据情况采取行动。当用户重新加载页面或打开第二个选项卡时,这也会起作用。关闭并重新打开浏览器(或选项卡)将创建一个新会话,有效地重置您的会话计时器。