Question

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

Was it helpful?

Solution 2

after some tries to fix it I didn't get any success... so I decided to go other way and prepared some kind of custom redirection through the redirect page that does the JavaScript location change.

Here is the code...

Helper method:

public static void CustomRedirect(this HttpResponseBase response, string url)
{
    string customRedirectPage = ConfigurationManager.AppSettings["custom_redirect_page"];

    if (string.IsNullOrEmpty(customRedirectPage))
        response.Redirect(url);
    else
    {
        byte[] bytesToEncode = Encoding.UTF8.GetBytes(url);
        string base64 = Convert.ToBase64String(bytesToEncode);
        response.Redirect(string.Format("{0}?url={1}", customRedirectPage, base64));
    }
}

Web.config:

<add key="custom_redirect_page" value="/Redirect/RedirectTo"/>

Controller:

    public ActionResult RedirectTo(string url)
    {
        return View((object)System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(url)));
    }

And HTML:

@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () {
            var url = '@Html.Raw(Model)';
            window.location = url;
        });
    </script>
}

Don't like this kind of hacks but that worked for me...

OTHER TIPS

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;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top