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

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

问题描述

我使用 c# 控制器作为网络服务.

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

其中我想将用户重定向到外部网址.

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

我该怎么做?

尝试过:

System.Web.HttpContext.Current.Response.Redirect

但是没有用.

推荐答案

使用控制器的 Redirect() 方法.

Use the Controller's Redirect() method.

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

更新

您不能直接从 ajax 响应执行服务器端重定向.但是,您可以使用新 url 返回 JsonResult 并使用 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;
});

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

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