ASP.NET MVC can I add Request filter to track previous page?

939 Views Asked by At

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 :)

3

There are 3 best solutions below

4
On BEST ANSWER

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.

2
On

You can get the refering page using Request.UrlReferrer

otherwise save the last url in a session-variable

like

Session["returnUrl"] = Request.RawUrl;
0
On

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.