actionresult 刷新当前页面
我想从某些操作方法返回一个强制当前页面刷新的结果。
我写这个是为了获得这样的结果:
public class RefreshResult : ActionResult {
public override void ExecuteResult(ControllerContext context) {
Uri referrer = context.HttpContext.Request.UrlReferrer;
if(referrer == null || string.IsNullOrEmpty(referrer.AbsoluteUri)) {
return;
}
context.HttpContext.Response.Redirect(referrer.AbsoluteUri);
}
}
在我的操作方法中,我只是返回新的 RefreshResult。它有效,但我很好奇这种方法可能存在的局限性。我不感兴趣为客户提供访问直接返回此类结果的操作方法的选项,因此我认为我始终能够以这种方式刷新当前页面。我说得对吗?
我在 stackoverflow 上发现了这个(以及其他几个问题): ActionResult 返回调用它的页面
但我更感兴趣的是可能这种方法的局限性,不在于“如何做”。
提前感谢
From some of the action methods I want to return a result that will force current page to refresh.
I wrote this to acquire such result:
public class RefreshResult : ActionResult {
public override void ExecuteResult(ControllerContext context) {
Uri referrer = context.HttpContext.Request.UrlReferrer;
if(referrer == null || string.IsNullOrEmpty(referrer.AbsoluteUri)) {
return;
}
context.HttpContext.Response.Redirect(referrer.AbsoluteUri);
}
}
In my action methods I simply return new RefreshResult. It works, but I am curious of the possible limitations of such approach. I am not intrested in giving customers an option to access action methods returning such results directly, so I think that I always will be able to refresh current page in such a way. Am I right?
I found this (and couple of other questions) on stackoverflow:
ActionResult return to page that called it
But I am more intrested in possible limitations of such approach, not in a "how to".
Thanx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
rouen的答案是刷新页面的一种方法。另一种是重定向回发送请求的 Url,不需要自己编写实现,只需在控制器中的正常操作中执行即可。
该操作可能如下所示
rouen answer is one way to refresh the page. The other one is to redirect back to the Url that the request was sent from, and there is no need to write implementation yourself, just do it in a normal action in a controller.
The Action might look like this
我不确定“将强制当前页面刷新的结果”是什么意思。如果您正在服务器上执行操作,则您已经在“刷新”页面。
如果您想要某种后重定向获取模式,以便再次通过 GET 操作“登陆”原始页面,则非常简单 - 只需实现从 RedirectToRouteResult 派生的自定义 ActionResult(由 RedirectToAction() 方法使用) Controller),并向其提供当前的路由值。
基于引荐来源网址的方法并不完全是坏事,但请记住,引荐来源网址是浏览器发送的标头,并且在某些客户端中可以是可选的(在浏览器等中禁用),而当前的路由值始终可供您使用。
I am not sure what do you mean by "result that will force current page to refresh". If you are executing action on server, you are already "refreshing" the page.
If what you want is some kind of post-redirect-get pattern in order to "land" on original page by GET action again, it is very easy - just implement your custom ActionResult as derived from RedirectToRouteResult (used by RedirectToAction() method on Controller), and supply it with current route values.
Your approach based on referrer is not entirely bad, but keep in mind that referrer is header sent by browser and can by optional in some clients (disabled in browser etc.), while current route values are always available to you.