关于asp.net mvc:Actionresult与JSONresult

Actionresult vs JSONresult

我有两个问题:

  • What is the difference between JSONResult and ActionResult?

  • When to use JSONResult in MVC?


  • ActionResult是操作可以返回的抽象类。

    Controller中的辅助方法(如Json()Content()View()…)返回继承ActionResult的不同具体类,包括JsonResult

    您应该将您的操作方法声明为返回ActionResult,这样它们就可以自由地返回任何具体的结果类。


    当您希望返回客户机(网页或移动客户机上的javascript)使用的原始JSON数据时,请使用JsonResult

    如果您想返回一个由浏览器处理的视图、重定向等,请使用ActionResult


    ActionResult是一个抽象类,JsonResultActionResult的子类。所以我们可以返回两种类型的JSON内容。


    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.

    这篇文章很有帮助http://brendan.enrick.com/post/types-of-aspnet-mvc-3-action-results.aspx


    根据ActionResult的msdn文件:

    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

    对于jsonresult:

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