An interviewer asked me this question. As far as my understanding of MVC architecture goes, that ActionResult is an abstract class from which all classes inherit.
But he asked specifically that which one of the action will be lightweight?
public ActionResult ar() { return Json(); }
OR
public JsonResult ar() { return Json(); }
Use JsonResult when you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client). Use ActionResult if you want to return a view, redirect etc to be handled by a browser.
The ActionResult is defined in the controller and the controller returns to the client (the browser). What is JsonResult ? JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).
To return JSON (JavaScript Object Notation) content from controller, we use JsonResult return type in the action method of the controller.
Json(Object, String, JsonRequestBehavior) Creates a JsonResult object that serializes the specified object to JavaScript Object Notation (JSON) format using the specified content type and JSON request behavior.
I'm not the biggest C# or MVC expert, but, this is a trick question.
They are the same.
You are correct that ActionResult is an abstract class and JsonResult inherits from it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With