TECHNOLOGY QUESTION -DETAILS WITH ANSWERS: (1)
How to detect Page Refresh in Server Side Code in ASP.NET MVC Controller
I need to carry out certain activity when the page is loaded first time and avoid this on page refresh. Is there any way to detect browser page refresh (F5)?
Question ID:000035 Submitted by: Surinder Kumar on 7/19/2017 Associated Tags:
Answered By Answer - ID: 00041
Simon Goodwin On: 7/19/2017
Answer On the Server side, I found one of the easiest ways to check if the request that just landed, is actually a refresh of the existing page that was already rendered to the browser.
I achieved the desired result by implementing following code in one of my controller method: -

 if (Session["prevURL"] == null)
            {
                Session["prevURL"] = Request.Url;
            }
            else
            {
                if ((Uri)Session["prevURL"] == Request.Url)
                {
                    return null;
                }
                else
                {
                    Session["prevURL"] = Request.Url;
                }
            }


Within the code, we are checking if for a particular session, the request is for the first time, we just store the requested url in the Session variable. We do nothing other that this here.

Upon second request, we check if the requested url matches the one placed in Session. If such is the case, it is a Refresh request. We either return null from a custom function or do whatever needed.

If the requested url does not match with the one stored in Session, it is a new page request. The new page url gets stored in Session in this case.

Provide Your Answer