(a)
var person = repository.GetPerson(id)
return person;
(b)
var person = repository.GetPerson(id)
return Ok(person);
According to the developer tools the result is the same and the status code is always 200. Is there a difference? If there is no difference, should I use the Ok() method or OkResult anyway?
Assuming both are examples are code inside an endpoint for an HTTP GET call, they are effectively the same.
There might be a slight difference in implementation, but the net result will be the same: the payloads in either case are identical (e.g. JSON or some other representation), and the status code will be identical, 200
.
It's a good practice to be explicit though, and use return Ok(person)
because this is both succinct and reveals the intent more obviously than return person
. You could also do return new OkObjectResult(person)
, which is intention revealing but not succinct. (ref: OkObjectResult).
Finally, OkResult should only be used when you don't want to return a payload. This is equivalent to return Ok()
.