0
/// <summary>
/// Get collection of movies as json.
/// </summary>
/// <returns></returns>
public ActionResult GetAllMovies1s()
{
    // db-> DBContext
    // Get the collection of movies.
    var movies = db.Movies.ToList();

    return Json(movies);
}

/// <summary>
/// Get collection of movies as json.
/// </summary>
/// <returns></returns>
public JsonResult GetAllMovies2()
{
    // db-> DBContext
    // Get the collection of movies.
    var movies = db.Movies.ToList();

    return Json(movies);
}

I am little bit novice to this,above two methods return json array to view to get movie JSON result to Ajax call.I know above two methods are fine to do the job done.But I want to know witch method is the better among them ?I think the ActionResult is better than JsonResult,since I heard about that when we write a function it is always good to return more generic return type (JsonResult is is concreate version of ActionResult).So Experts Please Help me to clarify choose good one among them ?(Sorry for poor English)

Please Guys Please read the question properly before make it as duplicate I already aware that ActionResult is more general version of JsonResult.My question is to know witch one is conventionally best to do the job done as clean way of coding and performance ...etc.

Prageeth godage
  • 4,054
  • 3
  • 29
  • 45
  • 2
    Both give exactly the same result, but using `JsonResult` makes it clearer what the return type of the method is. –  Apr 12 '16 at 05:18
  • Stephen,The your point is that second one is better since it more clean is that the only point?Denis,I think you are not understand my question properly. – Prageeth godage Apr 12 '16 at 05:26
  • I personally prefer it because specifies exactly what the intent of the method is - i.e. it will return json. But it really comes down to personal preference. –  Apr 12 '16 at 05:29
  • Duplicate: http://stackoverflow.com/questions/15250941/ as per @StephenMuecke's comment on the answer – Rob Apr 12 '16 at 05:31
  • ActionResult is nothing but an abstract class. It depends some programmers follow ActionResult for all ans some specify exact result type. – Anupam Singh Apr 12 '16 at 06:09

1 Answers1

3

I think that in this case there is no difference. if you use the method only in one case then use JsonResult. You will see that you are using. Accompany such code will be easier. Code written by you does not cause questions. ActionResult more general class and is an abstract class that an action can return.

if the code is used only in Ajax then use JsonResult else ActionResult. Looking at the hierarchy we will see the difference.

Inheritance Hierarchy for ActionResult:

ActionResult Inheritance Hierarchy

and for JsonResult

enter image description here

According for the ActionResult from MSDN:

The ActionResult class Encapsulates the result of an action method and is used to perform a framework-level operation on behalf of the action method.

According for the JsonResult from MSDN:

Represents a class that is used to send JSON-formatted content to the response.


JsonResult is is concrete version of ActionResult - then use JsonResult for Ajax call.

Sorry for my English.

Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54