Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ActionResult class methods in API

I was looking at a code example in this website and I noticed that in one endpoint it used Ok() return type and in the other one it just returned the object. When should I use ActionResult's methods such as Ok(), Created(), ... and when should we just return the object?

enter image description here

like image 463
brk Avatar asked May 03 '26 01:05

brk


1 Answers

You can use Ok(result) in the second case if you like. There is an implicit cast from T to ActionResult<T> that does the same as Ok(value), and this is the code that casts Employee in result to ActionResult<Employee>.

There is no such implicit cast to ActionResult which is used in the first case. It's not really possible to create an cast that makes sense.

If the first method returned Task<IEnumerable<Employee>>> then you could rely on the implicit cast and avoid using Ok().

like image 81
Martin Liversage Avatar answered May 04 '26 14:05

Martin Liversage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!