Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:27:02+00:00 2026-05-27T04:27:02+00:00

I understand that are similar question on StackOverflow about this problem, but none of

  • 0

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:27:02+00:00Added an answer on May 27, 2026 at 4:27 am

    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.
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a similar question about getting just the rotation, but as I understand
I understand that this is a very broad question, but a short it depends
There are lots of questions that ask this or a similar question. They all
I'll preface this question with the note that I have looked at this similar
I know that on stackoverflow there are many similar questions but mine is a
(I understand that someone else asked a similar question and it was closed as
I'm pretty new to Objective-C. I've read through a similar question but I can't
I understand that they are both supposed to be small, but what are the
I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I understand that requests are served by different threads, but do they all come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.