Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alieniasty/b6697b0eadc94921a4fa6dc1ef9cf645 to your computer and use it in GitHub Desktop.
Save alieniasty/b6697b0eadc94921a4fa6dc1ef9cf645 to your computer and use it in GitHub Desktop.
Here 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.
But here are some other advantages of using IHttpActionResult worth mentioning:
Respecting single responsibility principle: cause action methods to have the responsibility of serving the HTTP requests and does not involve them in creating the HTTP response messages.
Useful implementations already defined in the System.Web.Http.Results namely: Ok NotFound Exception Unauthorized BadRequest Conflict Redirect InvalidModelState (link to full list)
Uses Async and Await by default.
Easy to create own ActionResult just by implementing ExecuteAsync method.
you can use ResponseMessageResult ResponseMessage(HttpResponseMessage response) to convert HttpResponseMessage to IHttpActionResult.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment