asp.net-mvc Actionresult 与 JSONresult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15250941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:37:38  来源:igfitidea点击:

Actionresult vs JSONresult

asp.net-mvcasp.net-mvc-3asp.net-mvc-4asp.net-mvc-5jsonresult

提问by Sameer More

I have 2 questions:

我有两个问题:

  1. What is the difference between JSONResult and ActionResult?

  2. When to use JSONResult in MVC?

  1. JSONResult 和 ActionResult 有什么区别?

  2. 什么时候在 MVC 中使用 JSONResult?

回答by SLaks

ActionResultis an abstract class that an action can return.

ActionResult是一个动作可以返回的抽象类。

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

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

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

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

回答by Trevor Pilley

Use JsonResultwhen you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client).

JsonResult当您想要返回由客户端(网页或移动客户端上的 JavaScript)使用的原始 JSON 数据时使用。

Use ActionResultif you want to return a view, redirect etc to be handled by a browser.

使用ActionResult,如果你想返回一个视图,重定向等,以通过浏览器来处理。

回答by Mahara jothi

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

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

回答by Andrew

According to the MSDN documentationfor the ActionResult:

按照MSDN文档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

ActionResult 类封装了一个动作方法的结果,用于代表动作方法执行框架级的操作。

动作方法通过执行工作并返回动作结果来响应用户输入。动作结果表示框架将代表动作方法执行的命令。ActionResult 类是动作结果的基类

And for JsonResult:

对于JsonResult

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

表示用于将 JSON 格式的内容发送到响应的类。

回答by Sirwan Afifi

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.

结果

这个有点复杂,但仍然不是很复杂。它也对它的 ContentType 进行了硬编码,但使它更复杂的是它使用硬编码的 JavaScriptSerializer 在将 JSON 数据直接写入响应之前对其进行序列化。

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

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