Actionresult vs JSONresult

后端 未结 5 1207
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 07:07

I have 2 questions:

  1. What is the difference between JSONResult and ActionResult?

  2. When to use JSONResult in MVC?

相关标签:
5条回答
  • 2020-12-14 07:13

    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.

    0 讨论(0)
  • 2020-12-14 07:18

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

    0 讨论(0)
  • 2020-12-14 07:23

    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.

    0 讨论(0)
  • 2020-12-14 07:25

    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

    0 讨论(0)
  • 2020-12-14 07:34

    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.

    0 讨论(0)
提交回复
热议问题