tags:

views:

701

answers:

3

On ASP.net MVC, what is the "correct" way to have a controller return a 301 Redirect to an external site?

The various RedirectTo-Function seem to only return either relative links or routes that i have mapped manually, but there is no way to say "Perform a 301 Redirect to http://example.com".

I think I could just set Response.StatusCode or use Response.Redirect, but is that the way it should be done in MVC? Or is there an official "correct way" of performing redirects?

Update: In the meantime, I wrote an ActionResult for that: PermanentRedirectResult

A: 

Does that help any? http://blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx

It looks like you would just need to replace the virtual path by an URL of your liking.

Tomalak
Problem with that approach: It seems I have to add the Route to my routes, but since I do not know the target Url before the Controller Action, I can not put it in the routes table, and since the urls are one-time use only, i do not want to add unneccessary routes into the table.
Michael Stum
+1  A: 

Since you are talking about redirecting to a URL that you do not know anything about the routing (be that an internal page (think classic ASP.NET), or some external resource) then the only thing you can do is set the status code, and send the user's browser on its way.

At this chain in MVC you are worried about getting the user to their View, but that View is not under your control and may not even be a View at all. So ultimately you are doing this best you can in this circumstance, and to my knowledge are not breaking any MVC "rule".

I hope that helps.

Jason Whitehorn
That's the approach i've chosen now, i'm setting Response.StatusCode to 301 and Response.RedirectLocation to the target URL
Michael Stum
+1  A: 
Zack Peterson