2

(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?

3 Answers 3

6

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().

Sign up to request clarification or add additional context in comments.

Comments

4

Another difference is caused by the two implicit conversions on ActionResult<T>.

One is from the regular ActionResult. Ok("string) is an ActionResult (more specifically an OkObjectResult). So a method that returns ActionResult<User> will allow return Ok("string").

The second implicit conversion is from T. So return new User() will also be implicitly converted to ActionResult<User>.

This means that, while Ok(object) is very useful and arguably more explicit, the compiler is more likely to surface a type error if you avoid using Ok() and stick to returning the actual object.

Comments

0

The difference between return object and return ok(object) is i think pre-defined way to telling the status we are going to return in response to a webapi call.

return Ok(response) : In Below Block of Code webapi is returning OK(Response) that gives products list with OK that means Status of api call is predined with HTTP Status CODE 200. In case of any Error. There are many other return types like Unauthorized( ), Bad Request( ), NotFound() etc that defines the return type we are returning from webapi. In these cases In case of any Exception you need to handle it using exception handling or other method and pass the return type using other return types like badrequest or unauthorized

 [HttpGet("{id}")]
 public IActionResult Get(int id)
 {
     if(id==0)     
     return BadRequest();

     var response = _products.Single(x => x.Id == id);
     return Ok(response);
 }

return response : In Below Block of code we are not using return Ok (Object) here we are not defining any response type for webapi endpoint in this case result will be same but you will have less control on HTTP Status code and error response. Here is WEB API will decide the HTTP status code or response code to return depending on result / exception. you need to define the response code to return

 [HttpGet("{id}")]
 public Productsmodel Get(int id)
 {
     var response = _products.Single(x => x.Id == id);
     return response;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.