ObjectResult 和 JsonResult 有什么区别
What is difference between ObjectResult and JsonResult
Microsoft.AspNetCore.Mvc
命名空间中有两个 类:
ObjectResult
和 JsonResult
。
两者都将返回的对象转换为JSON格式。
它们有什么区别,使用它们的目的是什么?
JsonResult
是一个 IActionResult
,它将给定的 object 格式化为 JSON
ObjectResult
是一个内置了内容协商的 IActionResult
。
在其 ExecuteResultAsync
内部,负责写入响应流,框架将遍历可用的格式化程序和 select 相关的格式化程序。
选择格式化程序的逻辑类似于 ASP.NET Web API 中的逻辑,并基于以下优先顺序:
- 接受header
- Content-Type header
- select离子基于类型匹配
OkObjectResult Class
An Microsoft.AspNetCore.Mvc.ObjectResult
that when executed performs
content negotiation, formats the entity body, and will produce a
Microsoft.AspNetCore.Http.StatusCodes.Status200OK
response if
negotiation and formatting succeed.
参考文献:
Microsoft.AspNetCore.Mvc
命名空间中有两个 类:
ObjectResult
和 JsonResult
。
两者都将返回的对象转换为JSON格式。
它们有什么区别,使用它们的目的是什么?
JsonResult
是一个 IActionResult
,它将给定的 object 格式化为 JSON
ObjectResult
是一个内置了内容协商的 IActionResult
。
在其 ExecuteResultAsync
内部,负责写入响应流,框架将遍历可用的格式化程序和 select 相关的格式化程序。
选择格式化程序的逻辑类似于 ASP.NET Web API 中的逻辑,并基于以下优先顺序:
- 接受header
- Content-Type header
- select离子基于类型匹配
OkObjectResult Class
An
Microsoft.AspNetCore.Mvc.ObjectResult
that when executed performs content negotiation, formats the entity body, and will produce aMicrosoft.AspNetCore.Http.StatusCodes.Status200OK
response if negotiation and formatting succeed.
参考文献: