return Json will redirects to another view when Url specified

22,917

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
22,917
SivaRajini
Author by

SivaRajini

Passionate to write code in jquery and MVC/C#.having curiosity to learn advanced concepts in jquery as well MVC

Updated on July 10, 2022

Comments

  • SivaRajini
    SivaRajini almost 2 years

    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.

    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 ?

    for this case why i cant use return RedirectToAction("sep/employee").

    how return Json code redirects to action which specified in the Url.

    ex:

    public ActionResult Index()
    {
    ................................
    
    return Json(new { Url = "sep/employee"}
    }
    
    public ActionResult employee()
    {
    ....................
    }
    

    what is the difference b/s redirectaction and return Json