Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Json through ActionResult return type action or JsonResult return type action. Which one is lightweight or fast?

Tags:

c#

asp.net-mvc

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(); }
like image 216
Lakshay Avatar asked Mar 06 '14 04:03

Lakshay


People also ask

What is the difference between ActionResult and JsonResult?

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.

Can we return JSON in ActionResult?

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).

How do I return JSON from action method?

To return JSON (JavaScript Object Notation) content from controller, we use JsonResult return type in the action method of the controller.

Which method of JSON result is used to create JSON result?

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.


1 Answers

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.

like image 180
Rowan Freeman Avatar answered Nov 16 '22 02:11

Rowan Freeman