return Json will redirects to another view when Url specified

前端 未结 3 1882
礼貌的吻别
礼貌的吻别 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:06

    You return the following line to an ajax success call

    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;
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-22 22:32

    RedirectToAction simply 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 internals are:

    1. Construct redirection url from parameters passed to RedirectToAction
    2. Return new RedirectToRouteResult
    3. In ExecuteResult of RedirectToRouteResult you can find the following line:

      context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);

    which is merely returning 302 with redirection URL. More info - look at source code here.

    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:

    $.ajax({
        url: 'sep/employee',
        type: 'POST'
        success: function(result) {
            // handle the result from the server, i.e. process returned JSON data
        }
    });
    

    ExecuteResult of JsonResult just 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.

    EDIT:

    You of course can mimic 302 redirection by returning Json like

    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.

    0 讨论(0)
提交回复
热议问题