return Json will redirects to another view when Url specified

前端 未结 3 1995
礼貌的吻别
礼貌的吻别 2021-01-22 22:01

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

3条回答
  •  忘掉有多难
    2021-01-22 22:20

    whether it will redirect to specified action ? how it redirects to the URl ?

    I assume you mean to ask, "will it redirect to specified action? how will it redirect the the URI?"

    To answer your question: How it redirects to the 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 actually redirect to the URL that you received. So your view should have something like this:

    $.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?

    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.

提交回复
热议问题