188

I'm trying to redirect to external url from an action method but can't get it to work. Can anybody shed some light on my error?

public void ID(string id)
    {
        string url = string.Empty;
        switch (id)
        {
            case "DB2FCB11-579F-4DA2-A68C-A6495B9BAAB5":

                url = "http://www.somesite.com";
                break;
        }
        Response.Redirect(url, true);
    }

Thanks, Chris

1
  • 1
    What's your error? Or symptom? Does this go anywhere? Commented Oct 10, 2009 at 23:31

3 Answers 3

380

If you're talking about ASP.NET MVC then you should have a controller method that returns the following:

return Redirect("http://www.google.com");

Otherwise we need more info on the error you're getting in the redirect. I'd step through to make sure the url isn't empty.

Sign up to request clarification or add additional context in comments.

6 Comments

Optionally, you can do this instead: return new RedirectResult("yourURL", true); which is almost exactly the same, but gives you the parameter at the end to indicate whether it's a permanent redirect or not (HTTP 301 vs. something else, 307 maybe?)
@Mr.Pichler Most likely 302, but you could always check with Fiddler.
Redirect(rul) is 302 and RedirectPermanent(url) is 301. Check: stackoverflow.com/questions/17517318/…
Any thoughts on making this work with a local HTML file? Doesn't work: > return Redirect("C:/Users/Me/Documents/test.html");
|
16

Using JavaScript

 public ActionResult Index()
 {
    return Content("<script>window.location = 'http://www.example.com';</script>");
 }

Note: As @Jeremy Ray Brown said , This is not the best option but you might find useful in some situations.

Hope this helps.

5 Comments

Something like this can work, but a controller decides what action to perform. You don't need a script to be involved.
Believe it or not, something like this approach did help me. We have a hybrid ASP.NET MVC / AngularJS application with a lot of older web forms code all over the place. I used something similar to redirect to a URL that uses Angular routing. Because Angular routing follows a # mark in the URL, it is only recognized client-side, so Redirect cannot be utilized for such URLs.
@TNT Yes you are correct , I just provided this as an option, which might be useful for others.
Sorry if I wasn't polite @stom. I use this approach when I need to run some script with the redirect, like count one visit at Google Analytics.
This finally works. @Yuriy 's straightforward redirect solution failed for me. For security reasons requests heading to our MVC application must crawl through proxy server - IIS with Rewrite addin + rewrite rule. This combination probably messes with redirect URL. So ie. when I want to redirect from example.com/action to example.org/?search=xyz, using return Redirect("https://www.example.org/?search=xyz"); the result is redirecting to example.com/?search=xyz.
2

Maybe the solution someone is looking for is this:

Response.Redirect("/Sucesso")

This work when used in the View as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.