c# - 如何从 C# Controller 重定向到外部 URL

标签 c# asp.net-mvc web-services

我正在使用 C# Controller 作为网络服务。

在其中我想将用户重定向到外部 url。

我该怎么做?

尝试过:

System.Web.HttpContext.Current.Response.Redirect

但是没用。

最佳答案

使用 Controller 的 Redirect()方法。

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

更新

您不能直接从 ajax 响应执行服务器端重定向。但是,您可以返回带有新 url 的 JsonResult 并使用 javascript 执行重定向。

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

$.post("@Url.Action("YourAction")", function(data) {
    window.location = data.url;
});

关于c# - 如何从 C# Controller 重定向到外部 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9739170/

相关文章:

c# - 无法在 varchar 错误上调用方法

c# - WPF 容器 : equal width for elements but with spacing between them

c# - 混淆了? C#中的运算符

asp.net-mvc - MVCContrib 网格在空时显示标题?

asp.net-mvc - 如何在 MVC 的 Controller 级别使用 ValidateAntiForgeryToken?

asp.net - 如何以编程方式更改 div 的样式

c# - 使用 ServiceStack 使用 REST web 服务时的用户身份验证

c# - 确保文件从 Windows Phone 上传到 ASP.NET MVC 3

java - 如何为并行 Web 服务请求配置线程池?

java - 将方法发布为 Web 服务