zsharp
zsharp

Reputation: 13756

Adding a "previous" link

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 (?)

Upvotes: 0

Views: 147

Answers (3)

eu-ge-ne
eu-ge-ne

Reputation: 28153

Put this somewhere in your Base Controller or Custom Filter:

TempData["PreviousPage"] = TempData["CurrentPage"];
TempData["CurrentPage"] = Request.Url;

Upvotes: 0

John Rudy
John Rudy

Reputation: 37850

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.

Upvotes: 1

Michał Chaniewski
Michał Chaniewski

Reputation: 4806

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.

Upvotes: 1

Related Questions