185

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

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
user135498
  • 6,013
  • 8
  • 29
  • 29

4 Answers4

376

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.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • 5
    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?) – ctb May 13 '14 at 15:09
  • @Mr.Pichler Most likely 302, but you could always check with Fiddler. – Yuriy Faktorovich Aug 06 '15 at 18:34
  • 2
    Redirect(rul) is 302 and RedirectPermanent(url) is 301. Check: http://stackoverflow.com/questions/17517318/redirect-vs-redirectpermanent-in-asp-net-mvc – TNT Sep 29 '16 at 18:03
  • Any thoughts on making this work with a local HTML file? Doesn't work: > return Redirect("C:/Users/Me/Documents/test.html"); – Steve Eggering May 14 '18 at 19:19
  • 2
    @SteveEggering check out https://stackoverflow.com/questions/10830212/how-to-serve-html-file-from-another-directory-as-actionresult – Yuriy Faktorovich May 14 '18 at 20:03
  • What about a hosted API on Azure? – Berend Hulshof Jun 08 '18 at 08:27
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.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • 2
    Something like this can work, but a controller decides what action to perform. You don't need a script to be involved. – Jeremy Ray Brown Feb 07 '16 at 00:56
  • 6
    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. – thesentiment Apr 06 '16 at 21:13
  • @TNT Yes you are correct , I just provided this as an option, which might be useful for others. – Shaiju T Oct 01 '16 at 06:37
  • 2
    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. – TNT Oct 11 '16 at 19:47
  • 2
    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 https://example.com/action to https://www.example.org/?search=xyz, using `return Redirect("https://www.example.org/?search=xyz");` the result is redirecting to https://example.com/?search=xyz. – Kraken101 Nov 01 '17 at 11:40
2

Maybe the solution someone is looking for is this:

Response.Redirect("/Sucesso")

This work when used in the View as well.

-5

Try this (I've used Home controller and Index View):

return RedirectToAction("Index", "Home");
Yilmazam
  • 57
  • 7