Redirect on absolute URL doesn't work in MVC 4

Mr. Pumpkin picture Mr. Pumpkin · Mar 28, 2013 · Viewed 12.4k times · Source

For some reasons I have to redirect requests from my MVC 4 application to the pages with absolute URLs that's located in another domains. Here is the the code that I use:

public ActionResult Test(string url)
{
    return Redirect(url);
}

Everything works fine when I try it on my local machine, but when I publish the code to the production and try to get it working there then I have some problems... for example, to redirect request to the 'https://stackoverflow.com/questions/ask' it will be redirected to the 'http://{{myserverdomain.com}}/questions/ask'. So the request will be redirected to the local path 'questions/ask' instead of absolute URL.

Have no idea what and where I should check. I would appreciate any hints what could be the problem and where to check it...

Just in case: the server is Windows Server 2008 R2 Enterprise

UPDATE

URL/HTML encoding is not the reason of problem. Changing method to the

public ActionResult Test()
{
    return Redirect("https://stackoverflow.com/questions/ask");
}

will give the same result... it will be redirected to 'questions/ask'/ Suspect that the reason with the URL Rewrite module, but don't know how to check it yet...

SOLUTION

here is the link that helped to solve the issue: http://forums.iis.net/t/1171047.aspx

Answer

Joan Caron picture Joan Caron · Mar 28, 2013

It's strange because it's the right way...

public ActionResult YourAction()
{
    // ...
    return Redirect("http://www.example.com");
}

You can't directly perform a server side redirect from an ajax response. You could, however, return a JsonResult with the new url and perform the redirect with javascript.

Server Side :

public ActionResult YourAction()
{
    // ...
    return Json(new {url = "http://www.example.com"});
}

Client Side:

$.post("@Url.Action("YourAction")", function(data) {
    window.location = data.url;
});