Actionresult or Jsonresult returning Json?

Asked

Viewed 269 times

3

I see many codes in Controller where it is used as a method ActionResult returning Json(), but I also see an approach using the JsonResult logically returning Json.

Is there any difference between these two approaches? Is there a correct pattern to use?

1 answer

5


In class JsonResult we have an inheritance of ActionResult:

public class JsonResult : ActionResult

If you choose to return one JsonResult you will have to return ONLY JsonResult, choosing the approach ActionResult you have more flexibility to return a Action OR JSON for a Action is a ActionResult and JSON also because it inherits ActionResult.

In my opinion it is good to use ActionResult when you have a chance to return a Action OR JSON. If you are sure that the return should always be JSON utilize JsonResult.

  • 1

    Wouldn’t that be the opposite ? since Jsonresult inherits from Actionresult ....

  • 1

    got it, I saw in the documentation that Actionresult is an Abstract class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.