I have been developing with WebApi and have moved on to WebApi2 where Microsoft has introduced a new IHttpActionResult
Interface that seems to recommended to be used over returning a HttpResponseMessage
. I am confused on the advantages of this new Interface. It seems to mainly just provide a SLIGHTLY easier way to create a HttpResponseMessage
.
I would make the argument that this is "abstraction for the sake of abstraction". Am I missing something? What is the real world advantages I get from using this new Interface besides maybe saving a line of code?
Old way (WebApi):
public HttpResponseMessage Delete(int id)
{
var status = _Repository.DeleteCustomer(id);
if (status)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
New Way (WebApi2):
public IHttpActionResult Delete(int id)
{
var status = _Repository.DeleteCustomer(id);
if (status)
{
//return new HttpResponseMessage(HttpStatusCode.OK);
return Ok();
}
else
{
//throw new HttpResponseException(HttpStatusCode.NotFound);
return NotFound();
}
}
We have the following benefits of using
IHttpActionResult
overHttpResponseMessage
:IHttpActionResult
we are only concentrating on the data to be send not on the status code. So here the code will be cleaner and very easy to maintain.async
andawait
by default.The Web API basically return 4 type of object:
void
,HttpResponseMessage
,IHttpActionResult
, and other strong types. The first version of the Web API returnsHttpResponseMessage
which is pretty straight forward HTTP response message.The
IHttpActionResult
was introduced by WebAPI 2 which is a kind of wrap ofHttpResponseMessage
. It contains theExecuteAsync()
method to create anHttpResponseMessage
. It simplifies unit testing of your controller.Other return type are kind of strong typed classes serialized by the Web API using a media formatter into the response body. The drawback was you cannot directly return an error code such as a 404. All you can do is throwing an
HttpResponseException
error.This is just my personal opinion and folks from web API team can probably articulate it better but here is my 2c.
First of all, I think it is not a question of one over another. You can use them both depending on what you want to do in your action method but in order to understand the real power of
IHttpActionResult
, you will probably need to step outside those convenient helper methods ofApiController
such asOk
,NotFound
, etc.Basically, I think a class implementing
IHttpActionResult
as a factory ofHttpResponseMessage
. 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. Same here.If you want to return a response which needs to be constructed through a complex logic, say lots of response headers, etc, you can abstract all those logic into an action result class implementing
IHttpActionResult
and use it in multiple action methods to return response.Another advantage of using
IHttpActionResult
as return type is that it makes ASP.NET Web API action method similar to MVC. You can return any action result without getting caught in media formatters.Of course, as noted by Darrel, you can chain action results and create a powerful micro-pipeline similar to message handlers themselves in the API pipeline. This you will need depending on the complexity of your action method.
Long story short - it is not
IHttpActionResult
versusHttpResponseMessage
. Basically, it is how you want to create the response. Do it yourself or through a factory.I'd rather implement TaskExecuteAsync interface function for IHttpActionResult. Something like:
, where _request is the HttpRequest and _respContent is the payload.
Here are several benefits of
IHttpActionResult
overHttpResponseMessage
mentioned in Microsoft ASP.Net Documentation:But here are some other advantages of using
IHttpActionResult
worth mentioning:Ok
NotFound
Exception
Unauthorized
BadRequest
Conflict
Redirect
InvalidModelState
(link to full list)ExecuteAsync
method.ResponseMessageResult ResponseMessage(HttpResponseMessage response)
to convert HttpResponseMessage to IHttpActionResult.You might decide not to use
IHttpActionResult
because your existing code builds aHttpResponseMessage
that doesn't fit one of the canned responses. You can however adaptHttpResponseMessage
toIHttpActionResult
using the canned response ofResponseMessage
. It took me a while to figure this out, so I wanted to post it showing that you don't necesarily have to choose one or the other:Note,
ResponseMessage
is a method of the base classApiController
that your controller should inherit from.