I'm using the "Redirect After Post" (http://en.wikipedia.org/wiki/Post/Redirect/Get) pattern to solve the problems with refreshing that it solves, but I'm not seeing the URL change after the POST and subsequent GET.
我正在使用“重定向后发布”(http://en.wikipedia.org/wiki/Post/Redirect/Get)模式来解决它解决的刷新问题,但我没有看到URL更改后POST和随后的GET。
Here is my setup:
这是我的设置:
I have a form with some pretty extensive client-side validation, then submit.
我有一个表格,有一些非常广泛的客户端验证,然后提交。
@using (Html.BeginForm("AddItem", "Order", FormMethod.Post, new { Id = "addItemForm" }))
{
// form stuff
}
Client-side validation:
$('#addToOrder').click(function () {
// do a bunch of validation stuff.
}
if (criteriaMet) {
$('#addItemForm').submit();
}
"AddItem" controller:
public class OrderController {
[HttpPost]
public ActionResult AddItem(long? orderId, long menuItemId)
{
if (oneConditionIsTrue)
{
return RedirectToRoute("NamedRoute1", new { RouteValueDictionary values });
}
else
{
return RedirectToRoute("NamedRoute2", new { RouteValueDictionary values });
}
}
public class NamedRouteController
{
public ActionResult NamedRouteAction
{
// do some stuff
if (mobile)
{
return View("MobileView", model);
}
else
{
return View("RegularView", model);
}
}
After redirecting from the POST action (AddItem), I can step things through the GET action to the return (either one). I would expect the URL in the browser after all of this to be http://mydomain.com/NamedRoute/NamedRouteAction but it's http://mydomain.com/Order/AddItem. Why is this? Shouldn't the RedirectToRoute change the URL?
从POST操作(AddItem)重定向后,我可以通过GET操作执行返回操作(任一操作)。我希望在所有这些之后浏览器中的URL是http://mydomain.com/NamedRoute/NamedRouteAction,但它是http://mydomain.com/Order/AddItem。为什么是这样? RedirectToRoute不应该更改URL吗?
What am I missing?
我错过了什么?
1 个解决方案
#1
7
I suspect that the controller action is somehow invoked with an AJAX request. For example this could happen if you are using jQuery Mobile or something. Or maybe there's some other script that you have written doing this - it hijacks the form submission and sends an AJAX request instead. And since it is an AJAX request, you cannot possibly expect that the url in the client browser would ever change - that's the whole point of AJAX - stay on the same page.
我怀疑控制器动作是以某种方式使用AJAX请求调用的。例如,如果您使用jQuery Mobile或其他东西,可能会发生这种情况。或者也许你已经编写了一些其他脚本 - 它劫持了表单提交并发送了一个AJAX请求。因为它是一个AJAX请求,所以你不可能期望客户端浏览器中的url会发生变化 - 这就是AJAX的重点 - 保持在同一页面上。
This could be very easily verified by using a javascript debugging tool such as FireBug. Simply look at the Network tab and see if the POST request was an AJAX request. In the Net
tab find the request and see if there's the following request header:
通过使用诸如FireBug之类的javascript调试工具,可以非常轻松地验证这一点。只需查看“网络”选项卡,查看POST请求是否为AJAX请求。在Net选项卡中找到请求,看看是否有以下请求标头:
X-Requested-With: XMLHttpRequest
jQuery appends this HTTP header to all AJAX requests it sends.
jQuery将此HTTP标头附加到它发送的所有AJAX请求。
So basically if you expect a redirect to happen after a POST request you shouldn't use AJAX to submit your form. Or to be more precise: the redirect happens on the server side (once again you will be able to see it in FireBug - the 302 status code) and then the XMLHttpRequest simply follows this redirect but the client browser will not change its current location.
因此,基本上如果您希望在POST请求之后发生重定向,则不应使用AJAX来提交表单。或者更确切地说:重定向发生在服务器端(再一次,您将能够在FireBug中看到它 - 302状态代码),然后XMLHttpRequest只是遵循此重定向,但客户端浏览器不会更改其当前位置。
#1
7
I suspect that the controller action is somehow invoked with an AJAX request. For example this could happen if you are using jQuery Mobile or something. Or maybe there's some other script that you have written doing this - it hijacks the form submission and sends an AJAX request instead. And since it is an AJAX request, you cannot possibly expect that the url in the client browser would ever change - that's the whole point of AJAX - stay on the same page.
我怀疑控制器动作是以某种方式使用AJAX请求调用的。例如,如果您使用jQuery Mobile或其他东西,可能会发生这种情况。或者也许你已经编写了一些其他脚本 - 它劫持了表单提交并发送了一个AJAX请求。因为它是一个AJAX请求,所以你不可能期望客户端浏览器中的url会发生变化 - 这就是AJAX的重点 - 保持在同一页面上。
This could be very easily verified by using a javascript debugging tool such as FireBug. Simply look at the Network tab and see if the POST request was an AJAX request. In the Net
tab find the request and see if there's the following request header:
通过使用诸如FireBug之类的javascript调试工具,可以非常轻松地验证这一点。只需查看“网络”选项卡,查看POST请求是否为AJAX请求。在Net选项卡中找到请求,看看是否有以下请求标头:
X-Requested-With: XMLHttpRequest
jQuery appends this HTTP header to all AJAX requests it sends.
jQuery将此HTTP标头附加到它发送的所有AJAX请求。
So basically if you expect a redirect to happen after a POST request you shouldn't use AJAX to submit your form. Or to be more precise: the redirect happens on the server side (once again you will be able to see it in FireBug - the 302 status code) and then the XMLHttpRequest simply follows this redirect but the client browser will not change its current location.
因此,基本上如果您希望在POST请求之后发生重定向,则不应使用AJAX来提交表单。或者更确切地说:重定向发生在服务器端(再一次,您将能够在FireBug中看到它 - 302状态代码),然后XMLHttpRequest只是遵循此重定向,但客户端浏览器不会更改其当前位置。