Dimskiy
Dimskiy

Reputation: 5301

How to redirect users based on what page they came from?

I have a page (Controller Action that renders a View) that a user could navigate to from 3 different pages. Basically, a user gets there, does a few selections and clicks on Save button. At this point, I need to redirect the user to where he came from.

I'm wondering, what's the best practice to do that?

I know, for example, I could look in Request, figure out where he came from, then redirect back to there... But that doesn't look like the ideal approach to me.

Upvotes: 2

Views: 5748

Answers (2)

halfer
halfer

Reputation: 20430

(Posted on behalf of the question author).

This is what I ended up doing.

Controller:

public ActionResult Index()
{
   ViewBag.Referrer = Request.UrlReferrer.LocalPath;
   //.....
   return View();
}

View (Razor syntax):

<a href="@ViewBag.Referrer">Back</a>

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

I'm wondering, what's the best practice to do that?

Pass a returnUrl parameter when invoking this action. Store it in a hidden field if necessary. Make sure that the controller action that performs the validation and needs to redirect gets this parameter as action argument somehow and when the time comes return Redirect(returnUrl);

Upvotes: 7

Related Questions