tags:

views:

59

answers:

3

What the appropriate way to do this?

ViewData["PreviousPage"]=Request.UrlReferrer.PathAndQuery;

this doesnt work if directly accessing.

EDIT: I did a null check on Request.UrlReferrer, seems to be fine (?)

+1  A: 

If directly, it's impossible this way. URL referer is set only when clicking a link.

If you're interested only in "Previous Page" link working inside your website, then you can store current URL in session, and retrieve it during next request, then replace with a new current url. Ugly, but working.

Michał Chaniewski
+1  A: 

Is there some reason this needs to be server-side instead of client-side? If you can deal with client side, Javascript is the answer:

<input type=button value="Back" onClick="history.go(-1)">

This uses the browser's built-in back functionality -- it essentially mimics clicking the "Back" button.

John Rudy
A: 

Put this somewhere in your Base Controller or Custom Filter:

TempData["PreviousPage"] = TempData["CurrentPage"];
TempData["CurrentPage"] = Request.Url;
eu-ge-ne