Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A smart way to handle Return URLs in an MVC environment

A problem I come up against again and again is handling redirection to the previous page after a user runs some action such as clicking a 'Back To ...' link or saving the record they are editing.

Previously whenever I have needed to know what page to return to, I would provide a returnURL parameter to my current view.

http://blah.com/account/edit/1?returnURL="account/index"

This isn't a very clean way of handling this situation, as sometimes the return URL contains parameters such as search strings, etc, which have to be included in the URL.

http://blah.com/account/edit/1?returnURL="account/index?search="searchTerm""

Also, this creates an issue when a user can go another page forward before coming back to the page with the returnURL because you have to pass the returnURL through all visited pages.

Simply calling the browser's Back functionality isn't really sufficient either, because you might want the page to refresh, e.g. to show the edits you just saved in the previous page.

So my question is, has anyone found a smart way to handle this kind of situation, specifically in an MVC environment?

Note: I am using ASP .NET MVC so if possible I'd like answers to pertain to that, however any ideas are welcome.

like image 905
dnatoli Avatar asked Sep 12 '11 01:09

dnatoli


People also ask

How do you handle return URL?

Encode the returnUrl using Url. Encode(returnUrl) for inclusion in the URL. When ready to redirect, use Url. Decode(returnUrl) and use the value for the actual redirect.

What is return URL in MVC?

The Forms Authentication makes use of ReturnUrl parameter to redirect user to the requested page after Login in ASP.Net MVC.

How does MVC handle wrong URL?

If those URLs don't include a controller or action method name, then you can provide the missing information through default values on your routes. But if those URLs include an incorrect action or controller name, then ASP.NET MVC will simply return one of the standard HTTP error codes.

What is return view in MVC?

The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About.


2 Answers

What's wrong with setting a cookie, or using a session variable? The only reason you wouldn't is if you don't control the page that calls into you, in which case your only options are query strings, post values, or referrer.

like image 150
Erik Funkenbusch Avatar answered Oct 19 '22 11:10

Erik Funkenbusch


I thought I might add my answer to the question to see if others think it's a good idea.

I'm simply passing it through to the controller using TempViewData:

@{
   TempData["returnURL"] = Request.Url.AbsoluteUri;
}

and then accessing it in a similar way to this (in my real version I check that the key is in TempData and that the returnURL is a real URL):

return Redirect(TempData["returnURL"].ToString());

If it needs to continue on past the first page change (i.e. Search page -> Edit page -> Edit Section page) I'm adding it again

TempData["returnURL"] = TempData["returnURL"];
like image 39
dnatoli Avatar answered Oct 19 '22 11:10

dnatoli