how to redirect to external url from c# controller

后端 未结 3 741
一整个雨季
一整个雨季 2020-12-02 17:54

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.Ht         


        
3条回答
  •  抹茶落季
    2020-12-02 18:29

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

提交回复
热议问题