Various Types of Action Results in WEB API 2


WEB API 2 is a framework for building HTTP services that can be accessed from any client including browsers and mobile devices. It is an ideal platform for building RESTful applications on the .NET Framework.

HTTP is not just for serving up web pages. HTTP is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop applications. ASP.NET Web API is a framework for building web APIs on top of the .NET Framework. The following are the return types for Web API action methods.

This content describes how Web API converts the return value from a controller action into an HTTP response message. Below are the various types:

• Void
• HttpResponseMessage
• IHttpActionResult
• Some other type

The Web API action methods, depending on the user requirements above, can be used at different circumstances.


Users have to Prerequisite things about the different types of response while working with Web API, as such, you should keep in mind the return types like Response Status Code, Content type and the output.



If the End User is using the Void as the return type for the action method, Web API will just return an empty HTTP response with status Code 204. For Instance:

Web API Controller:




From the above output, we can clearly see you will not get the data from the service and the status code will be 204 in all situations with No Content.


If the User is using the ‘HttpResponseMessage’ it will return an HttpResponseMessage, Web API converts the return value directly into an HTTP response message, using the properties of the HttpResponseMessage object to populate the response. We can customize the response by providing status code, content type, and data to be returned to HttpResponseMessage. For Instance:



From the above screen shots, we can observe the status code 200 Ok and Content Type as JSON. We can return any complex type data using HttpResponseMessage.


This type is newly introduced in Web API 2.  It’s much higher than HttpResponseMessage but IHttpActionResult
Uses the HttpResponseMessage by providing the user with the desired customization and reusability.
IHttpActionResult contains ExecuteAsync method to create an instance of HttpResponseMessage asynchronously. We have to add some code logic in the implemented class as per our requirement. When user/client calls the action method of IHttpActionResult type, Web API automatically hits the ExecuteAsync method. Below are some advantages

• 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.


Some other type

Web API action method can contain any entity type as the return type. This uses a media formatter to serialize the return values. Then Web API writes the serialized value into the response body.
The Status Code is 200 (ok).



There is a drawback to this approach – you cannot directly return an error code, such as 404. However, you can throw an HttpResponseException for error codes.