Why should I use IHttpActionResult instead of HttpResponseMessage?

Technology CommunityCategory: ASP.NET Web APIWhy should I use IHttpActionResult instead of HttpResponseMessage?
VietMX Staff asked 3 years ago

ere are several benefits of IHttpActionResult over HttpResponseMessage mentioned in Microsoft ASP.Net Documentation:

  • Simplifies unit testing your controllers.
  • Moves common logic for creating HTTP responses into separate classes.
  • Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.

Also a class implementing IHttpActionResult as a factory of HttpResponseMessage. With that mind set, it now becomes an object that need to be returned and a factory that produces it. In general programming sense, you can create the object yourself in certain cases and in certain cases, you need a factory to do that.

Long story short – it is not IHttpActionResult versus HttpResponseMessage. Basically, it is how you want to create the response.