Return 307 Temporary Redirect in ASP.NET MVC
Asked Answered
F

3

11

Is it possible to return a 307 Temporary Redirect from a controller in ASP.NET MVC?

I sometimes need to re-POST the values submitted from one form to another URI.

Using JavaScript to do the selection on the client side (thereby bypassing this issue) is not an option.

Redirecting via a GET is not an option as posted data includes an 8k string which is likely to mean that the URI would be too long for some (many?) browsers.

Is this even possible?

Floozy answered 29/10, 2009 at 14:15 Comment(0)
A
2

Take a look at the following article - you can use the same technique for 307:

301 Redirects

Anacardiaceous answered 29/10, 2009 at 14:20 Comment(5)
I need to sometimes make the redirects based on the data that's passed. Accessing the model (where the logic is) from the routehandler seems wrong. Having [a copy of] the logic in the routehandler seems even worse.Floozy
Using a RouteHandler seems like a nice way to solve this kind of problem though. - Just not when there's logic involved.Floozy
You could create a different action result in which case - again here's an example that you can modify: stum.de/2008/10/22/permanentredirectresultAnacardiaceous
I've ended up doing some redesign to avoid this whole issue. Some great solutions though. Thanks FinnNkFloozy
Breaks the requirement "sometime" which implies logic is required first. Not bad as a general purpose solution but doesn't answer this question.Obcordate
C
16

To return a 307 redirect result from an MVC action, use the following:

public ActionResult Action()
{
    string url = GetRedirectUrl()
    HttpContext.Response.AddHeader("Location", url);
    return new HttpStatusCodeResult(307);
}
Coel answered 12/12, 2014 at 0:50 Comment(0)
P
5

ASP.NET Core:

 public RedirectResult (string url, bool permanent, bool preserveMethod);

So

return Redirect(url, false, false); // 302
return Redirect(url, true, false);  // 301
return Redirect(url, false, true);  // 307
return Redirect(url, true, true);   // 308
Plateau answered 11/1, 2020 at 7:57 Comment(0)
A
2

Take a look at the following article - you can use the same technique for 307:

301 Redirects

Anacardiaceous answered 29/10, 2009 at 14:20 Comment(5)
I need to sometimes make the redirects based on the data that's passed. Accessing the model (where the logic is) from the routehandler seems wrong. Having [a copy of] the logic in the routehandler seems even worse.Floozy
Using a RouteHandler seems like a nice way to solve this kind of problem though. - Just not when there's logic involved.Floozy
You could create a different action result in which case - again here's an example that you can modify: stum.de/2008/10/22/permanentredirectresultAnacardiaceous
I've ended up doing some redesign to avoid this whole issue. Some great solutions though. Thanks FinnNkFloozy
Breaks the requirement "sometime" which implies logic is required first. Not bad as a general purpose solution but doesn't answer this question.Obcordate

© 2022 - 2024 — McMap. All rights reserved.