English 中文(简体)
页: 1
原标题:asp.net mvc - Detecting page refresh

我理解,这个问题在StackOverflow上也存在类似问题,但没有任何问题解决了我的问题,因此产生了一个新的问题。

如标题所示,在用户复读一页时,我想发现。 我有一页,可以节省一些关于用户所做工作的记录信息(如添加、删除或替换物品)。 只有当用户离开网页时,而不是通过更新这一记录,才能保存这一记录。

我尝试了以下例子,以发现它是否再次提出或提出新的要求:

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();
}

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.

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?

最佳回答

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()));
    }
}

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

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

您不妨改进探测工作,以检查所采用的吉大港定居地保有权方法(作为《长期就业法》和《全球就业法》的参考)。 请注意,它使用ok进行检测。

问题回答

If you are using MVC 2 or 3 then the TempData does not expire on the subsequent request, but on the next read.

http://robertcorvus.com/warning-mvc-nets-tempdata-now-persists-across-screens/





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签