如何重定向到外部URL从C#控制器 [英] how to redirect to external url from c# controller

查看:301
本文介绍了如何重定向到外部URL从C#控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是C#控制器作为网络服务。

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

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

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

我该怎么办呢?

试过:

System.Web.HttpContext.Current.Response.Redirect

但它没有工作。

推荐答案

使用控制器的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirect.aspx\">Redirect()方法。

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

更新

您不能直接执行服务器端从Ajax响应重定向。你可以,但是,返回一个JsonResult新的URL,并用JavaScript执行重定向。

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

这篇关于如何重定向到外部URL从C#控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆