如何从 c # 控制器重定向到外部 URL

我使用 c # 控制器作为 Web 服务。

在其中,我想将用户重定向到一个外部 URL。

我该怎么做?

试过:

System.Web.HttpContext.Current.Response.Redirect

但没成功。

206532 次浏览

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;
});

Try this:

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

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/