What's the advantage or recommendation on using IActionResult
as the return type of a WebApi controller instead of the actual type you want to return?
Most of the examples I've seen return IActionResult
, but when I build my first site I exclusively use View Model classes as my return types.... now I feel like I did it all wrong!
The main advantage is that you can easily test your code using a mocking framework.
And as you build your controllers, you can easily change your return object as well. IActionResult is a interface and has many implementations like JsonResult, ViewResult, FileResult and so on.
The main advantage is that you can return error/status codes or redirects/resource urls.
For example:
or