I have ASP.NET MVC Project and I have some pages (let's call it Destination Page) that I can access from multiple pages. So I want to track which page redirects to Destination Page so I can return to it again. I red about Request Filters .. Can I use it in my case? Thanks in advance :)
-
If you want this client side [history.go(-1)](http://stackoverflow.com/questions/8067510/onclick-javascript-to-make-browser-go-back-to-previous-page) will do it for you – Liam Feb 18 '16 at 13:59
3 Answers
You can get the refering page using Request.UrlReferrer
otherwise save the last url in a session-variable
like
Session["returnUrl"] = Request.RawUrl;

- 1,270
- 9
- 13
-
I tried to use **"Request.UrlReferrer"** but it works only if the current request is initiated by clicking on a Link at the current Asp.net web application. Otherwise, if the request is redirected to a page using Response.Redirect, **"Request.UrlReferrer"** value to be null. – karim.fone Feb 18 '16 at 11:43
-
if you dont want to use querystring parameters like mentioned in another answer look at my edited answer – Nikolaj Zander Feb 18 '16 at 14:00
Just pass a return URL in the query string. In other words instead of redirecting like:
return RedirectToAction("Destination");
Do:
return RedirectToAction("Destination", new { returnUrl = Request.RawUrl });
Of course, your "Destination" action needs to accept this as a param:
public ActionResult Destination(Foo otherParam, string returnUrl)
Then, when you're done with whatever you're doing in "Destination", redirect back via:
if (!String.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Fallback");
The IsLocalUrl
check is to prevent query string tampering, by ensuring that the return URL is actually local (i.e. relative) to your site.

- 232,153
- 36
- 385
- 444
URL Referrer is only populated by an actual client-click (anchor tag, button).
Not when you manually put it in the URL (which is what my JavaScript is doing).
The solution i am doing to have to with is to create a cookie on the whatever.aspx page, and read that cookie from the JavaScript before i redirect again.

- 8,415
- 26
- 86
- 139
-
-
Definitely, multiple tabs will be a problem, and any solution that depends entirely on JavaScript is doomed to failure. JavaScript should only be used to enhance your application; your application should always still function completely without it. – Chris Pratt Feb 18 '16 at 13:58
-
*JavaScript is doomed to failure. JavaScript should only be used to enhance your application*...seriously. It's not 2010 anymore. What about grunt or angular/knockout single page apps, etc... – Liam Feb 18 '16 at 14:05
-
For something like Facebook, requiring JavaScript is understandable. For your average website, it's not. If you have some kind of actual web application that obviously is beyond the scope of HTML, then users understand and expect that they will need to allow JavaScript. However, lots of people still choose to disable JavaScript for most websites, and there are still screen readers and other assistive devices that do not fully support it. When at all possible, everything should be available and work without JavaScript. – Chris Pratt Feb 25 '16 at 13:50