title: asp.net mvc - Detecting page refresh
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I tried the example below to detect if it's a refresh or a new request:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>Considering it's a <code>TempData</code> it should expire on my next action. However, it's surviving the entire application for some reason. According to this <a href="http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications" rel="nofollow">blog</a> 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.</p>
  
  <p>I've been thinking about use the javascript function <code>onbeforeunload</code> 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?</p>