Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

json - What is difference between ObjectResult and JsonResult

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?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...