It is not possible to redirect and POST at the same time (not specific to ActionResult, it's simply not possible). The POST must come from a "user initiated" form post. If you need to trigger it from code, you should render said form to the browser and include
a javascript which posts it automatically.
The provided link is specific to Routing, but the interesting logic (setting the status code and adding the correct
Location header) is of more immediate interest to your application.
Marked as answer by ricka6 on Apr 03, 2010 01:50 AM
Last time I tried (actually it was also for a PayPal integration), 307:s didn't work at all. Now, after reading your post, I revisited it and it does indeed seem to work quite OK. Thanks for the reminder!
But, I still don't recommend it.
1. In Firefox, the user will get an alert stating that there is a redirect in order. The alert may confuse/scare users (although it is of course actually a good and correct thing that Firefox does)
2. If the user choses to cancel the redirect, the browser just ends up on an empty response page.
3. It will not work in some older browsers.
4. If all you're doing is reposting the same data, then why not just set the correct form action?
-- "Mark As Answer" if my reply helped you --
Marked as answer by rleonard on Apr 02, 2010 04:08 PM
What levib correctly pointed out was that you can set both the location and status for the redirect within the response message (accessible within the ActionResult's ExecuteResult method). The link levib provided shows how to do this, but not in the context
of an ActionResult subclass.
I've opted to take Gunteman's advice to not pursue this route, however, as the issue of Firefox (and other browsers?) alerting the user of a redirect just as they go to the PayPal page would likely alarm more than just a few?
I have confirmed that FireFox users are prompted with an alert requesting they approve of the redirect, but this is not the case with either IE8 or Safari 4. I agree with previous contributor to this thread that this prompt can be confusing to some.
rleonard
Member
5 Points
21 Posts
ActionResult subclass sending a POST
Apr 01, 2010 10:04 PM|LINK
Calling aContext.Response.Redirect(...) from an ActionResult subclass' ExecuteResult method results in an HTTP GET being sent to the specified URL.
Does someone know how to get the redirect to send an HTTP POST instead? This is necessary for passing control to PayPal's payment page.
actionresult
gunteman
All-Star
22406 Points
3305 Posts
Re: ActionResult subclass sending a POST
Apr 01, 2010 11:00 PM|LINK
It is not possible to redirect and POST at the same time (not specific to ActionResult, it's simply not possible). The POST must come from a "user initiated" form post. If you need to trigger it from code, you should render said form to the browser and include a javascript which posts it automatically.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: ActionResult subclass sending a POST
Apr 01, 2010 11:12 PM|LINK
You probably want an HTTP 307 redirect. See http://stackoverflow.com/questions/1644045/return-307-temporary-redirect-in-asp-net-mvc for more information.
The provided link is specific to Routing, but the interesting logic (setting the status code and adding the correct Location header) is of more immediate interest to your application.
gunteman
All-Star
22406 Points
3305 Posts
Re: ActionResult subclass sending a POST
Apr 02, 2010 12:32 AM|LINK
Last time I tried (actually it was also for a PayPal integration), 307:s didn't work at all. Now, after reading your post, I revisited it and it does indeed seem to work quite OK. Thanks for the reminder!
But, I still don't recommend it.
1. In Firefox, the user will get an alert stating that there is a redirect in order. The alert may confuse/scare users (although it is of course actually a good and correct thing that Firefox does)
2. If the user choses to cancel the redirect, the browser just ends up on an empty response page.
3. It will not work in some older browsers.
4. If all you're doing is reposting the same data, then why not just set the correct form action?
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: ActionResult subclass sending a POST
Apr 03, 2010 01:52 AM|LINK
what do you use to "redirect to send an HTTP POST"?
rleonard
Member
5 Points
21 Posts
Re: ActionResult subclass sending a POST
Apr 03, 2010 05:31 AM|LINK
What levib correctly pointed out was that you can set both the location and status for the redirect within the response message (accessible within the ActionResult's ExecuteResult method). The link levib provided shows how to do this, but not in the context of an ActionResult subclass.
I've opted to take Gunteman's advice to not pursue this route, however, as the issue of Firefox (and other browsers?) alerting the user of a redirect just as they go to the PayPal page would likely alarm more than just a few?
rleonard
Member
5 Points
21 Posts
Re: ActionResult subclass sending a POST
Apr 15, 2010 03:41 PM|LINK
Here's a code snippet from a simple class I wrote called PayPalActionResult:
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.StatusCode = 307;
context.HttpContext.Response.StatusDescription = "Temporary Redirect";
context.HttpContext.Response.RedirectLocation = "https://www.sandbox.paypal.com/cgi-bin/webscr";
}
I have confirmed that FireFox users are prompted with an alert requesting they approve of the redirect, but this is not the case with either IE8 or Safari 4. I agree with previous contributor to this thread that this prompt can be confusing to some.