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.
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.
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.
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.
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; });
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With