39

I have 2 questions:

  1. What is the difference between JSONResult and ActionResult?

  2. When to use JSONResult in MVC?

Community
  • 1
  • 1
Sameer More
  • 589
  • 1
  • 6
  • 13
  • you should see that question man :) http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it – Lemo Jun 13 '13 at 14:14

5 Answers5

39

ActionResult is an abstract class that an action can return.

The helper methods in Controller (eg, Json(), Content(), View(), ...) return different concrete classes that inherit ActionResult, including JsonResult.

You should declare your action methods as returning ActionResult, so that they have the freedom to return any concrete result class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    [MSDN lists the different concrete implementations of ActionResult](http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx) – Donal Lafferty Jan 21 '14 at 14:46
  • 3
    `You should declare your action methods as returning ActionResult, so that they have the freedom to return any concrete result class` I disagree. You should be specific as possible about return types, and as general as possible for arguments. `JsonResult` is the proper type to return in most cases, as other methods may use the method and want to inspect `.Data` which is only exposed in `JsonResult`. – Rob Apr 12 '16 at 05:31
  • @Rob: You should not call action methods directly. If you need to expose a value to other code, move it to a helper method. – SLaks Apr 12 '16 at 14:28
  • 1
    @SLaks While I agree in general that it might be worthy of a helper method, it's entirely acceptable for a test to call the method directly. If you define your method as returning JSON, and one day you decide to return XML, you'll catch it *early* via tests. Or you might think twice about changing an API which used to always return JSON. It's a bit of a code smell to return either `View()` or `Json()` on the same controller method. Return types should always be specific, whether or not they should be called directly. – Rob Apr 14 '16 at 08:19
20

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.

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
5

ActionResult is an abstract class .JsonResult is subtype of ActionResult. So we can return json content in both types.

Mustafa ASAN
  • 3,747
  • 2
  • 23
  • 34
Mahara jothi
  • 59
  • 1
  • 2
1

According to the MSDN documentation for the ActionResult:

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.

An action method responds to user input by performing work and returning an action result. An action result represents a command that the framework will perform on behalf of the action method. The ActionResult class is the base class for action results

And for JsonResult:

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

Andrew
  • 5,395
  • 1
  • 27
  • 47
1

JsonResult

This one is a bit more complex, but still not very. It also has hardcoded its ContentType, but what makes it a bit more complex is that it uses a hardcoded JavaScriptSerializer to serialize the JSON data before writing it directly to the response.

this post can be helpful
http://brendan.enrick.com/post/types-of-aspnet-mvc-3-action-results.aspx

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110