0

I need to prepend a domain to the redirect performed in RedirectToAction, so it's not relative, but an absolute url. How can this be accomplished?

EDIT: I'll add a bit more information: It's a multitenant application that allows for clients to setup proxydomains. So I need all the urls to go through the proxy. So an actionredirect that would be: http://domainX.com/Question/Preview/640328 needs to go through http://domainY.com/SUBDOMAIN/Question/Preview/640328

tereško
  • 58,060
  • 25
  • 98
  • 150
dgivoni
  • 535
  • 2
  • 7
  • 17
  • do you want to redirect to another action or to a completly different website? – da_berni Apr 29 '14 at 12:00
  • possible duplicate of [How can I get my webapp's base URL in ASP.NET MVC?](http://stackoverflow.com/questions/1288046/how-can-i-get-my-webapps-base-url-in-asp-net-mvc) – Ufuk Hacıoğulları Apr 29 '14 at 12:01
  • It's a multitenant application that allows for clients to setup proxydomains. So the action/controller does exist and it will be redirected correctly, but I need all the urls to go through the proxy. – dgivoni Apr 29 '14 at 12:07

2 Answers2

2

This can't be done using RedirectToAction on it's own. However, the UrlHelper.RouteUrl method can be used to generate absolute URLs using a specific host name.

You simply need to generate the URL using UrlHelper.RouteUrl, then perform the redirect using the Redirect method.

If you know the route name, use something like the following:

var routeValues = new RouteValueDictionary(new { id = 12345 });
string url = Url.RouteUrl("Products.Show", routeValues, "http", "www.domainname1.com");
return Redirect(url);

If you want to generate the URL based on the controller name and action, use the following:

var routeValues = new RouteValueDictionary(
    new { controller = "Products", action = "Show", id = 12345 });
string url = Url.RouteUrl(null, routeValues), "http", "www.domainname1.com");
return Redirect(url);

Either of the above would redirect to an absolute URL like http://www.domainname1.com/products/12345

Note that an instance of UrlHelper is available within controller code via the built-in Controller.Url property).

Dan Malcolm
  • 4,382
  • 2
  • 33
  • 27
0

By definition redirect to action must take you to an action in one of your controllers. If you want to redirect to another url try:

return Redirect("http://www.stackoverflow.com");
JTMon
  • 3,189
  • 22
  • 24
  • Yes, the problem is that it's a multitenant application that allows for clients to setup proxydomains. So the action/controller does exist and it will be redirected correctly, but I need all the urls to go through the proxy. – dgivoni Apr 29 '14 at 12:05