Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to redirect to external url from c# controller

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.

like image 425
Elad Benda Avatar asked Mar 16 '12 14:03

Elad Benda


People also ask

How do I redirect a URL in spring boot?

We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.

How do I use response redirect?

Response. Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister. aspx" page and it has a button that redirects you to the "UserDetail.

What is HTTP Response redirect?

In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3 , and a Location header holding the URL to redirect to. When browsers receive a redirect, they immediately load the new URL provided in the Location header.


2 Answers

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; }); 
like image 95
jrummell Avatar answered Sep 17 '22 03:09

jrummell


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/

like image 27
EndlessSpace Avatar answered Sep 20 '22 03:09

EndlessSpace