当指定 Url 时,返回 Json 将重定向到另一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21039680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return Json will redirects to another view when Url specified
提问by SivaRajini
When you do return Json(...) you are specifically telling MVC not to use a view, and to serve serialized JSON data. Your browser opens a download dialog because it doesn't know what to do with this data.
当您确实返回 Json(...) 时,您特别告诉 MVC 不要使用视图,并提供序列化的 JSON 数据。您的浏览器会打开一个下载对话框,因为它不知道如何处理这些数据。
i got the above information from below link
我从下面的链接中获得了上述信息
How to return Json object from MVC controller to view
when i give this below code in some action result
当我在某些操作结果中给出以下代码时
return Json(new { Url = "sep/employee"}
whether it will redirect to specified action ? how it redirects to the URl ?
是否会重定向到指定的动作?它如何重定向到 URl ?
for this case why i cant use return RedirectToAction("sep/employee").
对于这种情况,为什么我不能使用 return RedirectToAction("sep/employee")。
how return Jsoncode redirects to action which specified in the Url.
返回 Json代码如何重定向到 Url 中指定的操作。
ex:
前任:
public ActionResult Index()
{
................................
return Json(new { Url = "sep/employee"}
}
public ActionResult employee()
{
....................
}
what is the difference b/s redirectaction and return Json
b/s 重定向和返回 Json 有什么区别
回答by Nitin Varpe
You return the following line to an ajaxsuccesscall
您将以下行返回到ajaxsuccess呼叫
return Json(new { Url = "sep/employee"});
you then need to specify where to redirect to the new page
然后您需要指定重定向到新页面的位置
success: function(result) {
window.location.href=result.Url;
}
回答by Marcin Wachulski
RedirectToActionsimply returns 302 code to your browser with URL telling where the browser should redirect to. The browser immediately makes yet another call to the server to that URL obtained from redirection response.
RedirectToAction只需将 302 代码返回到您的浏览器,并带有 URL 告诉浏览器应该重定向到哪里。浏览器立即向服务器再次调用从重定向响应中获得的 URL。
RedirectToAction internals are:
RedirectToAction 内部结构是:
- Construct redirection url from parameters passed to
RedirectToAction - Return new
RedirectToRouteResult In
ExecuteResultofRedirectToRouteResultyou can find the following line:context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
- 从传递给的参数构造重定向 url
RedirectToAction - 返回新的
RedirectToRouteResult 在
ExecuteResult中RedirectToRouteResult,你可以找到以下行:context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
which is merely returning 302 with redirection URL. More info - look at source code here.
这只是返回带有重定向 URL 的 302。更多信息 - 在此处查看源代码。
Returning JSON data simply returns JSON serialized object to your browser. Is not meant to do any redirect. To consume such a result you will likely call the server using $.ajax:
返回 JSON 数据只是将 JSON 序列化对象返回到您的浏览器。不打算做任何重定向。要使用这样的结果,您可能会使用$.ajax以下命令调用服务器:
$.ajax({
url: 'sep/employee',
type: 'POST'
success: function(result) {
// handle the result from the server, i.e. process returned JSON data
}
});
ExecuteResultof JsonResultjust serializes passed CLR object to the response stream, which lands in your browser after response is fully received. Then you can handle such response in JavaScript code.
ExecuteResult的JsonResult只是将传递的 CLR 对象序列化到响应流,在完全接收到响应后,响应流会出现在您的浏览器中。然后您可以在 JavaScript 代码中处理此类响应。
EDIT:
编辑:
You of course can mimic 302 redirection by returning Json like
您当然可以通过返回 Json 来模拟 302 重定向
return Json(new { Url = "redirectionUrl"}
and at client side handle it like
并在客户端处理它
$.ajax({
url: 'sep/employee',
type: 'POST'
success: function(result) {
// mimic the 302 redirection
windows.location.href = result.Url
}
});
although IMHO it should be avoided since you reinvent MVC infrastructure and enlarge your code base.
尽管恕我直言,应该避免它,因为您重新发明了 MVC 基础架构并扩大了代码库。
回答by Ronald
whether it will redirect to specified action ? how it redirects to the URl ?
是否会重定向到指定的动作?它如何重定向到 URl ?
I assume you mean to ask, "will it redirect to specified action? how will it redirect the the URI?"
我假设您的意思是问,“它会重定向到指定的操作吗?它将如何重定向 URI?”
To answer your question: How it redirects to the URL?
回答您的问题:它如何重定向到 URL?
In your example it will redirect to the URL, if you made the HTTP request as AJAX and that you will explicitly handle the resulting HTTP response to actuallyredirect to the URL that you received. So your view should have something like this:
在您的示例中,它会重定向到 URL,如果您以 AJAX 形式发出 HTTP 请求,并且您将显式处理生成的 HTTP 响应以实际重定向到您收到的 URL。所以你的观点应该是这样的:
$.ajax({
url: '{your controller}/index',
type: 'GET'
success: function(url) {
// write code to redirect to the URL
// example:
// window.navigate(url)
}
});
If your view does not have anything that, then it will not redirect. You did not post your view, so I am assuming it just a normal page load.
如果您的视图没有任何内容,则它不会重定向。你没有发表你的观点,所以我假设它只是一个正常的页面加载。
Your next question, what is the difference b/s redirection and return Json?
你的下一个问题,b/s 重定向和返回 Json 有什么区别?
If you really just want to redirect then do RedirectToAction from your controller, that is exactly what it is for. You can do the same effect using AJAX and JSON to be able to redirect, but AJAX and JSON serves a much wider purpose than just redirecting and in most cases (unless you have very good reasons) you probably will not want replace RedirectToAction with that approach.
如果您真的只想重定向,那么从您的控制器执行 RedirectToAction,这正是它的用途。您可以使用 AJAX 和 JSON 来实现相同的效果以进行重定向,但 AJAX 和 JSON 的用途远不止重定向,而且在大多数情况下(除非您有很好的理由),您可能不想用这种方法替换 RedirectToAction .

