ResponseRedirect作为Json

use*_*104 0 c# asp.net-mvc asp.net-mvc-3

如何创建一个返回ResponseRedirect的控制器函数,而不是将ResponseRedirect作为Json对象返回?

我想做这样的事情

 return Json(new { url = RedirectToAction("AccountMyProducts", "Account"), redirect = "true" });
Run Code Online (Sandbox Code Playgroud)

在我的jsonobject中获取重定向url.

Rav*_*dag 7

这样做

return Json(data, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)

解释:函数返回JSONResult的类型,它由ActionResult继承.

  1. JsonRequestBehavior.AllowGet:
    从这里回答 为什么-jsonrequestbehavior-needed

这是为了防止使用HTTP GET返回数据的JSON请求进行非常具体的攻击.

基本上,如果您的操作方法不返回敏感数据,那么允许获取是安全的.

但是,MVC将此与DenyGet一起作为默认设置来保护您免受此攻击.在您决定通过HTTP GET公开之前,它会让您考虑所公开数据的含义

如果您打算根据json数据重定向

return Json(new 
{ 
    redirectUrl = Url.Action("AccountMyProducts", "Account"), 
    isredirection= true 
});
Run Code Online (Sandbox Code Playgroud)

在Jquery成功回调函数中,这样做

$.ajax({
.... //some other stuffs including url, type, content type. 

//then for success function. 
success: function(json) {
    if (json.isredirection) {
        window.location.href = json.redirectUrl;
    }
}

});
Run Code Online (Sandbox Code Playgroud)