91

I'm using a c# controller as web-service.

In it I want to redirect the user to an external url.

How do I do it?

Tried:

System.Web.HttpContext.Current.Response.Redirect

but it didn't work.

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • I don't think you can do that - it's a cross-domain security violation. – Brian Driscoll Mar 16 '12 at 14:35
  • How are consumers consuming the web service? – Kirk Woll Mar 16 '12 at 14:35
  • Define "as web-service." How is the controller action being accessed? You can send a redirect response, but if that response isn't going to a standard web browser request then it'll probably be ignored. – David Mar 16 '12 at 14:36
  • @David I'm calling it from ajax POST. – Elad Benda Mar 16 '12 at 15:21
  • @EladBenda: In that case your JavaScript code will need to handle the redirect response. The browser won't do it automatically. Of course, the first Google result for this was a Stack Overflow question :) – David Mar 16 '12 at 15:38
  • possible duplicate of [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – David Mar 16 '12 at 15:38
  • I understood that from your previouse answer. A strange thing. I tried window.location.href (or without href) = 'https://www.yourdomain.com' and got 324 error. but when i typed https://www.yourdomain.com in the browser itself- I got the pgae OK – Elad Benda Mar 16 '12 at 15:45
  • I understood that from your previouse answer. A strange thing. I tried window.location = 'https://www.yourdomain.com' and got 324 error. but when i typed https://www.yourdomain.com in the browser itself- I got the pgae OK – Elad Benda Mar 16 '12 at 15:48
  • 1
    @EladBenda: I'm not sure what a 324 error actually means. I suspect that a JavaScript redirect should also contain the `http://` segment of the URL, though. – David Mar 16 '12 at 18:12
  • 1
    possible duplicate of [.net mvc redirect to external url](http://stackoverflow.com/questions/1549324/net-mvc-redirect-to-external-url) – KyleMit Feb 12 '15 at 18:24

3 Answers3

170

Use the Controller's Redirect() method.

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

Update

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.

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

$.post("@Url.Action("YourAction")", function(data) {
    window.location = data.url;
});
jrummell
  • 42,637
  • 17
  • 112
  • 171
21

If you are using MVC then it would be more appropriate to use RedirectResult instead of using Response.Redirect.

public ActionResult Index() {
        return new RedirectResult("http://www.website.com");
    }

Reference - https://blogs.msdn.microsoft.com/rickandy/2012/03/01/response-redirect-and-asp-net-mvc-do-not-mix/

EndlessSpace
  • 1,320
  • 2
  • 24
  • 46
16

Try this:

return Redirect("http://www.website.com");
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • 6
    note, you cannot use "www.website.com" - you must provide the http:// or https:// syntax – markthewizard1234 Oct 07 '16 at 11:44
  • 3
    Thank you @markthewizard1234 among all the answers ive browsed, your note was the one thing that set it straight for me. I did not give it a thought as i was using a dynamic redirect to intranet sites. – LuqJensen Dec 27 '16 at 22:32