何时在ActionResult上使用JsonResult [英] When to use JsonResult over ActionResult

查看:168
本文介绍了何时在ActionResult上使用JsonResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法找到有关此问题的具体答案.我已经查看了这个问题和其他地方的帖子和后续帖子,但我真正的想法是读到的是JsonResult具有硬编码的内容类型,实际上并没有任何性能提升.

如果两个结果都可以返回Json,您为什么会需要使用JsonResult而不是ActionResult.

public ActionResult()
{
    return Json(foo)
}

public JsonResult()
{
    return Json(bar)
}

谁能解释一下ActionResult根本无法完成工作而必须使用JsonResult 的情况.如果没有,为什么首先要存在JsonResult.

解决方案

何时在ActionResult上使用JsonResult

我通常会返回具体结果(例如JsonResultViewResult),还有一些优点:

Pro ASP.NET MVC 3 Framework 中有引号:

请注意,在 列表为ViewResult.该方法将编译并正常工作 如果我们指定了更通用的ActionResult类型.其实有些 MVC程序员将每种操作方法的结果定义为 ActionResult,即使他们知道它总是会返回更多 具体类型.我们在这方面特别勤奋 下面的示例使您清楚如何使用每个结果 类型,但在实际项目中我们往往会放松一些.

只有在动作应返回不同类型的结果时,我才会在具体选项上使用ActionResult.但这不是常见的情况.

我还想补充一点,ActionResult是一个抽象类,因此您不能简单地创建这种类型的实例并返回它. JsonResult是具体的,因此您可以创建其实例并从操作方法中返回. 有许多其他类型是从ActionResult 派生的,基本上它们都是使方法的数据类型尽可能具体或更通用是否更好?其中讨论了方法参数和返回值的最佳做法.. >

总体思路提供抽象类型作为参数,以便您的方法可以处理更大范围的参数; 返回足够具体的类型,以便您的客户无需强制转换或转换它们.

I've been unable to find a concrete answer regarding this question. I've viewed posts and subsequent posts from this question and elsewhere but all I've really come out of reading is that JsonResult has a hardcoded content type and there really isn't any performance gains.

If both results can return Json why would you need to use JsonResult over ActionResult.

public ActionResult()
{
    return Json(foo)
}

public JsonResult()
{
    return Json(bar)
}

Can anyone explain a scenario where ActionResult simply can't get the job done and JsonResult must be used. If not, why does JsonResult exist in the first place.

解决方案

When to use JsonResult over ActionResult

I usually return concrete results (e.g. JsonResult, ViewResult) and there are my pros:

There are some links where people support this approach:

There is a quote from Pro ASP.NET MVC 3 Framework:

Note Notice that that the return type for the action method in the listing is ViewResult. The method would compile and work just as well if we had specified the more general ActionResult type. In fact, some MVC programmers will define the result of every action method as ActionResult, even when they know it will always return a more specific type. We have been particularly diligent in this practice in the examples that follow to make it clear how you can use each result type, but we tend to be more relaxed in real projects.

I would use ActionResult over concrete one only if an action should return different types of results. But it is not that common situation.

I'd like also to add that ActionResult is an abstract class so you cannot simply create an instance of this type and return it. JsonResult is concrete so you can create its instance and return from an action method. There are many other types that derived from ActionResult and basically all of them are used to override ExecuteResult method.

public abstract class ActionResult
{
    public abstract void ExecuteResult(ControllerContext context);
} 

This method is invoked by ControllerActionInvoker and implements logic writing data to response object.

ControllerActionInvoker does not know about concrete results so that it can handle any result that is derived from ActionResult and implements ExecuteResult.

In both cases you return an instance of JsonResult type in your example and Json(model) it is simply a Factory Method that creates an instance of JsonResult.

There is another SO question Is it better to have a method's datatype be as specific as possible or more general? where best practices for method parameters and return values are discussed.

The general idea is providing abstract types as parameters so that your method can handle wider range of parameter; returning concrete enough types so that your clients do not need to cast or convert them.

这篇关于何时在ActionResult上使用JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆