asp.net mvc-检测页面刷新 [英] asp.net mvc - Detecting page refresh

查看:367
本文介绍了asp.net mvc-检测页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解在StackOverflow上有与此问题类似的问题,但是没有一个问题解决了我的问题,因此我正在创建一个新问题.

I understand that are similar question on StackOverflow about this problem, but none of it solved my issue, so i'm creating a new one.

正如标题所述,我想检测用户何时刷新页面.我有一个页面,我在其中保存一些有关用户执行的操作的日志信息(例如添加,删除或编辑项目).该日志只能在用户离开页面时保存,而不能通过刷新保存.

As the title says, i'd like to detect when an user refreshs a page. I have a page where i save some log information about what the user done on it (things like adding, removing or edting items). This log can only be saved when an user leaves the page, not by refreshing it.

我尝试了下面的示例来检测它是刷新还是新请求:

I tried the example below to detect if it's a refresh or a new request:

public ActionResult Index()
{
   var Model = new Database().GetLogInfo();
   var state = TempData["refresh"];

   if(state == null)
   {
    //This is a mock structure
    Model.SaveLog(params);
   }


TempData["refresh"] = true; //it can be anything here

return View();
}

考虑到它是TempData,它应该在我的下一个操作中到期.但是,由于某种原因,它可以使整个应用程序幸存下来.根据此博客它应在我的后续请求中过期(除非我不明白某事).即使我从应用程序注销并再次登录,我的TempData仍然有效.

Considering it's a TempData it should expire on my next action. However, it's surviving the entire application for some reason. According to this blog it should expire on my subsequent request (unless i'm not understandig something). Even if i log out from my app and log in again, my TempData is still alive.

我一直在考虑使用javascript函数onbeforeunload对某个动作进行AJAX调用,但我不得不再次依赖TempData或以某种方式保留此刷新信息.有提示吗?

I've been thinking about use the javascript function onbeforeunload to make an AJAX call to some action, but once again i'd have to rely on TempData or to persist this refresh info somehow. Any tips?

推荐答案

您可以使用如下所示的ActionFilter:

You can use a ActionFilter that looks something like this:

public class RefreshDetectFilter : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cookie = filterContext.HttpContext.Request.Cookies["RefreshFilter"];
        filterContext.RouteData.Values["IsRefreshed"] = cookie != null &&
                                                        cookie.Value == filterContext.HttpContext.Request.Url.ToString();
    }
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.SetCookie(new HttpCookie("RefreshFilter", filterContext.HttpContext.Request.Url.ToString()));
    }
}

global.asax中注册.然后,您可以在控制器中执行此操作:

Register it in global.asax. Then you can just do this in your controller:

if (RouteData.Values["IsRefreshed"] == true)
{
    // page has been refreshed.
}

您可能希望改进检测以检查所用的HTTP方法(因为POST和GET URL看起来相同).请注意,它使用cookie进行检测.

You might want to improve the detection to also check the HTTP Method used (as a POST and GET url can look the same). Do note that it uses a cookie for the detection.

这篇关于asp.net mvc-检测页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆