What is difference between ObjectResult and JsonResult

走远了吗. 提交于 2019-12-09 00:20:59

问题


There are two classes in Microsoft.AspNetCore.Mvc namespace:

ObjectResult and JsonResult.

Both convert the returned object in the JSON format.

What is difference between them and what is the purpose to use them?


回答1:


JsonResult is an IActionResult which formats the given object as JSON

ObjectResult is an IActionResult that has content negotiation built in.

Inside its ExecuteResultAsync, responsible for writing to the response stream, the framework will walk through the available formatters and select a relevant one.

The logic for choosing a formatter is similar to that in ASP.NET Web API, and based on the following order of precedence:

  • Accept header
  • Content-Type header
  • selection based on type match

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.

References:

  • Transitioning from ASP.NET Web API 2 to ASP.NET MVC 6
  • Asp.Net Documentation: JsonResult Class
  • Asp.Net Documentation: ObjectResult Class
  • Asp.Net Documentation: OkObjectResult Class


来源:https://stackoverflow.com/questions/38788559/what-is-difference-between-objectresult-and-jsonresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!