Reputation: 3439
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?
Upvotes: 50
Views: 28901
Reputation: 247451
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:
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.
References:
Upvotes: 73