A smart way to handle Return URLs in an MVC enviro

2020-07-02 11:28发布

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.

10条回答
神经病院院长
2楼-- · 2020-07-02 11:45

Would this be better handled by partial actions that display without leaving the page and using JQuery to make a dialog/wizard workflow?

Then you only need to react to the 'Finish' button on the dialog to refresh the original view.

查看更多
何必那么认真
3楼-- · 2020-07-02 11:46

For the part of your question regarding "saving the record they are editing" I would think the post-redirect-get (PGR) pattern would apply to you.

This might be a good place to read about it if you are not familiar with it.

查看更多
Animai°情兽
4楼-- · 2020-07-02 11:48

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"];
查看更多
做个烂人
5楼-- · 2020-07-02 11:50

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.

查看更多
forever°为你锁心
6楼-- · 2020-07-02 11:52

Try register a new route of which the url is /{controller}/{action}/{id}/returnurl/{*url} and then use a RedirectToAction in the action that accepts url as a parameter

查看更多
爷、活的狠高调
7楼-- · 2020-07-02 11:52
  1. Encode the returnUrl using Url.Encode(returnUrl) for inclusion in the URL.
  2. When ready to redirect, use Url.Decode(returnUrl) and use the value for the actual redirect.
查看更多
登录 后发表回答