Let's say I have a controller action that deletes an item out of a user's shopping basket. This controller action is triggered by performing a POST to the url ~/delete/{id}. If I have several pages on my application that will post to this url, how do I construct the controller action to redirect back to the page that posted to it?
假设我有一个控制器动作,从用户的购物篮中删除一个项目。此控制器操作通过对url ~/delete/{id}执行POST来触发。如果我的应用程序中有几个页面将会发送到这个url,那么我如何构造控制器动作来重定向回发送给它的页面?
6 个解决方案
#1
11
You should provide a RedirectToUrl parameter from the posting page.
您应该从发布页面提供一个RedirectToUrl参数。
Relying on referrer headers is not a good practice.
依靠引用头不是一个好的做法。
Instead, do something like this:
相反,你可以这样做:
public ActionResult Delete(int id, string RedirectToUrl)
{
// check if RedirectToUrl is null or empty and redirect accordingly
}
On the posting view or partial view you can provide the parameter in several ways:
在发布视图或部分视图中,可以通过以下几种方式提供参数:
<%= Html.Hidden("RedirecToUrl","/my/lovely/url") %>
or
或
<form action="/item/delete/22?RedirectToUrl=/my/lovely/url">
I'd prefer the first option.
我更喜欢第一种选择。
#2
11
That's what I do:
这就是我做的事情:
public ActionResult ResendActivationEmail()
{
// Do other things here
return new RedirectResult(Request.UrlReferrer.AbsoluteUri);
}
#3
2
The first thing that I would do is use Ajax.ActionLink, then if the user has Javascript turned on, you'd never actually leave the page. This is the best solution. If you don't want a link, you could also have an Ajax form. Either of these could use the DELETE or POST method.
我要做的第一件事就是使用Ajax。ActionLink,如果用户打开了Javascript,你永远不会离开这个页面。这是最好的解决方案。如果不需要链接,也可以使用Ajax表单。这两种方法都可以使用DELETE或POST方法。
In order to handle the case where Javascript is turned off, when you detect in the controller that the POST was not done with Ajax (Request.IsAjaxRequest is false), you could look at the Request.UrlReferer property to get the Url of the referring page. If this is not null, you can use a RedirectResult to go back to this page. If it is null, choose a default landing page -- probably something like "Your item has been removed, click here to continue shopping." This latter will probably only rarely get hit.
为了处理Javascript被关闭的情况,当您在控制器中检测到POST不是使用Ajax完成的(请求)。IsAjaxRequest为false),您可以查看请求。UrlReferer属性获取引用页面的Url。如果这不是null,您可以使用RedirectResult返回到该页。如果它是空的,选择一个默认的登录页面——比如“您的项目已经被删除,点击这里继续购物”。后者可能只会很少受到影响。
#4
0
I've never tried it but you can use the Referer header to know where the post or get comes from and try to match the URL with a route.
我从来没有尝试过,但是你可以使用Referer头来知道文章来自哪里,并尝试将URL与路径匹配。
#5
0
Just use the URL Referer [sic] header.
只需使用URL引用器[sic]头。
var requestFrom = Request.UrlReferrer
You can find the documentation at: http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx
您可以在http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx上找到文档
The only time this wouldn't work is when the page is requested directly, but in that case you wouldn't have any place to redirect to anyways.
唯一不能工作的情况是直接请求页面时,但是在这种情况下,您将没有任何位置重定向到任何方式。
There is also the option of doing the only thing async using AJAX, so that your Delete action only does what it describes and isn't responsible for doing something outside of its intended purpose of deleting.
还有一个选项是使用AJAX进行唯一的异步操作,因此您的Delete操作只执行它描述的操作,不负责执行超出其预期目的的删除操作。
#6
0
If you are using WCSF (Web Client Software Factory) to implement MVC pattern, you could use PageFlow to do all navigation.
如果您正在使用WCSF (Web客户端软件工厂)来实现MVC模式,您可以使用PageFlow来完成所有的导航。
Eg:-
如:-
PageFlow.Next(); or PageFlow.Previous();
PageFlow.Next();或PageFlow.Previous();
#1
11
You should provide a RedirectToUrl parameter from the posting page.
您应该从发布页面提供一个RedirectToUrl参数。
Relying on referrer headers is not a good practice.
依靠引用头不是一个好的做法。
Instead, do something like this:
相反,你可以这样做:
public ActionResult Delete(int id, string RedirectToUrl)
{
// check if RedirectToUrl is null or empty and redirect accordingly
}
On the posting view or partial view you can provide the parameter in several ways:
在发布视图或部分视图中,可以通过以下几种方式提供参数:
<%= Html.Hidden("RedirecToUrl","/my/lovely/url") %>
or
或
<form action="/item/delete/22?RedirectToUrl=/my/lovely/url">
I'd prefer the first option.
我更喜欢第一种选择。
#2
11
That's what I do:
这就是我做的事情:
public ActionResult ResendActivationEmail()
{
// Do other things here
return new RedirectResult(Request.UrlReferrer.AbsoluteUri);
}
#3
2
The first thing that I would do is use Ajax.ActionLink, then if the user has Javascript turned on, you'd never actually leave the page. This is the best solution. If you don't want a link, you could also have an Ajax form. Either of these could use the DELETE or POST method.
我要做的第一件事就是使用Ajax。ActionLink,如果用户打开了Javascript,你永远不会离开这个页面。这是最好的解决方案。如果不需要链接,也可以使用Ajax表单。这两种方法都可以使用DELETE或POST方法。
In order to handle the case where Javascript is turned off, when you detect in the controller that the POST was not done with Ajax (Request.IsAjaxRequest is false), you could look at the Request.UrlReferer property to get the Url of the referring page. If this is not null, you can use a RedirectResult to go back to this page. If it is null, choose a default landing page -- probably something like "Your item has been removed, click here to continue shopping." This latter will probably only rarely get hit.
为了处理Javascript被关闭的情况,当您在控制器中检测到POST不是使用Ajax完成的(请求)。IsAjaxRequest为false),您可以查看请求。UrlReferer属性获取引用页面的Url。如果这不是null,您可以使用RedirectResult返回到该页。如果它是空的,选择一个默认的登录页面——比如“您的项目已经被删除,点击这里继续购物”。后者可能只会很少受到影响。
#4
0
I've never tried it but you can use the Referer header to know where the post or get comes from and try to match the URL with a route.
我从来没有尝试过,但是你可以使用Referer头来知道文章来自哪里,并尝试将URL与路径匹配。
#5
0
Just use the URL Referer [sic] header.
只需使用URL引用器[sic]头。
var requestFrom = Request.UrlReferrer
You can find the documentation at: http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx
您可以在http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx上找到文档
The only time this wouldn't work is when the page is requested directly, but in that case you wouldn't have any place to redirect to anyways.
唯一不能工作的情况是直接请求页面时,但是在这种情况下,您将没有任何位置重定向到任何方式。
There is also the option of doing the only thing async using AJAX, so that your Delete action only does what it describes and isn't responsible for doing something outside of its intended purpose of deleting.
还有一个选项是使用AJAX进行唯一的异步操作,因此您的Delete操作只执行它描述的操作,不负责执行超出其预期目的的删除操作。
#6
0
If you are using WCSF (Web Client Software Factory) to implement MVC pattern, you could use PageFlow to do all navigation.
如果您正在使用WCSF (Web客户端软件工厂)来实现MVC模式,您可以使用PageFlow来完成所有的导航。
Eg:-
如:-
PageFlow.Next(); or PageFlow.Previous();
PageFlow.Next();或PageFlow.Previous();