在 MVC 环境中处理返回 URL 的智能方法
我一次又一次遇到的一个问题是,在用户运行某些操作(例如单击“返回...”链接或保存他们正在编辑的记录)后处理到上一页的重定向。
以前,每当我需要知道要返回哪个页面时,我都会向当前视图提供一个 returnURL
参数。
http://blah.com/account/edit/1?returnURL="account/index"
这不是处理这种情况的一种非常干净的方法,因为有时返回 URL 包含必须包含在 URL 中的参数,例如搜索字符串等。
http://blah.com/account/edit/1?returnURL="account/index?search="searchTerm""
此外,当用户在返回带有 returnURL
的页面之前可以前进另一个页面时,这会产生一个问题,因为您必须在所有访问的页面中传递 returnURL
。
简单地调用浏览器的后退功能也不够,因为您可能希望刷新页面,例如显示您刚刚在上一页中保存的编辑。
所以我的问题是,有没有人找到一种聪明的方法来处理这种情况,特别是在 MVC 环境中?
注意:我正在使用 ASP .NET MVC,因此如果可能的话,我希望获得与此相关的答案,但欢迎任何想法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
设置 cookie 或使用会话变量有什么问题?您不这样做的唯一原因是,如果您无法控制调用您的页面,在这种情况下,您唯一的选择是查询字符串、发布值或引荐来源网址。
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.
我想我可以将我的答案添加到这个问题中,看看其他人是否认为这是一个好主意。
我只是使用 TempViewData: 将其传递到控制器
,然后以与此类似的方式访问它(在我的真实版本中,我检查密钥是否在 TempData 中,并且
returnURL
是一个真实的 URL):如果需要在第一个页面更改之后继续(即搜索页面 -> 编辑页面 -> 编辑部分页面),我将再次添加它
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:
and then accessing it in a similar way to this (in my real version I check that the key is in
TempData
and that thereturnURL
is a real URL):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
查看我的博客文章: 使用 cookie 进行控制在asp.net mvc 3上登录后返回页面
就像@Mystere Man提到的那样,您可以只使用cookie或会话。不久前我遇到类似的情况时,我就去买饼干了。
Check my blog post on it: Using cookies to control return page after login on asp.net mvc 3
Just like @Mystere Man mentioned, you can just use a cookie or session for it. I went for cookies back when I had a similar situation a while ago.
尝试注册一个 url 为
/{controller}/{action}/{id}/returnurl/{*url}
的新路由,然后在接受 url 作为参数的操作Try register a new route of which the url is
/{controller}/{action}/{id}/returnurl/{*url}
and then use aRedirectToAction
in the action that accepts url as a parameter尽管我仍然认为您不应该创建自己的“后退”按钮。
though i'd still argue that you shouldn't be creating your own "back" button.
使用拦截器或切面:
@Before
切面)并将请求的 URL 保存到会话中,每次都覆盖它这种设计可以让您在需要使用时始终可以获取最新的请求。 这是一个示例 在 .NET 中编写切面/拦截器。另外,PostSharp 是一个 .NET 方面项目。
Use an interceptor or an aspect:
@Before
aspect) and save the requested URL to the session, overwriting it each timeThis kind of design allows you to always have the most recent request available if you want to use it. Here's an example to write an aspect / interceptor in .NET. Additionaly, PostSharp is a .NET aspect project.
目前,我还没有找到一种快速而肮脏的方法……所以我正在使用一种实用的方法。
在概念层面上,页面的“后退能力”应由您当前所在的页面决定。如果控制器中捕获的参数通过 ViewModel 传递给视图,则视图可以推断出这一点(在大多数情况下)。
示例:
访问
Foo
后,我将前往Bar
查看一些内容,并且后退按钮应该返回Foo
。控制器
ViewModels
视图(部分)
// 没有双关语意图...或者也许是。 :)
您的 BarView 现在可以从其模型解释它需要返回的位置(使用
fooId
)。在 BarView 上(使用 MVC2 语法):
您也可以使用 Html.ActionLink。
或者:
您可以从 BaseViewModel 继承 ViewModel,它可以具有受保护的属性
returnURL
。必要时设置此项。示例:
在 ViewModel 上:
在视图上:
At present, a quick and dirty method has eluded me... so I'm using a practical method.
On a conceptual level, the 'back-ability' of a page should be determined by the page that you're currently on. The View can infer this (in most cases) if the parameters captured in the Controller are passed to it via the ViewModel.
Example:
Having visited
Foo
, I'm going toBar
to view some stuff, and the back button should return toFoo
.Controller
ViewModels
View (Partial)
// No pun intended... or maybe it was. :)
Your BarView can now interpret from its model where it needs to go back to (using
fooId
).On your BarView (using MVC2 syntax):
You can use Html.ActionLink as well.
Alternatively:
You can inherit your ViewModels from a BaseViewModel, which can have a protected property
returnURL
. Set this where necessary.Example:
On your ViewModel:
On View:
通过在不离开页面的情况下显示的部分操作并使用 JQuery 制作对话框/向导工作流程可以更好地处理此问题吗?
然后您只需对对话框上的“完成”按钮做出反应即可刷新原始视图。
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.
对于您关于“保存他们正在编辑的记录”的问题,我认为重定向后获取(PGR)模式将适用于您。
如果您不熟悉它,这可能是阅读它的好地方。
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.
Url.Encode(returnUrl)
对 returnUrl 进行编码以包含在 URL 中。Url.Decode(returnUrl)
并使用实际重定向的值。Url.Encode(returnUrl)
for inclusion in the URL.Url.Decode(returnUrl)
and use the value for the actual redirect.