HttpResponseMessage
A HttpResponseMessage allows us to work with the HTTP protocol (for example, with the headers property) and unifies our return type. In simple words, an HttpResponseMessage is a way of returning a message/data from your action.
The following is a quick glimpse of that.
// GetEmployee action
public HttpResponseMessage GetEmployee(int id)
{
// Do Something
HttpResponseMessage response = new HttpResponseMessage();
// Customize your response here
return response;
}
Why to return a HttpResponseMessage
Since we can return primitive types or complex types to the user, why HttpResponseMessage?
Let's create a simple get method that will return the Employee data for the id provided.
public Employee GetEmployee(int id)
{
return EmployeeContext.Employees.FirstOrDefault(e => e.Id == id);
}
As you can see, the method above is fulfilling the requirement; it is returning the Employee details for the ID provided. But what if no such employee exists for the ID provided, it will return the null value with a successful response. Rather it would be more efficient if we return a 404 error, with a message like “Employee not found”.
Using HttpResponseMessage
Now let's modify our previous example to return a HttpResponseMessage for the Employee data for the ID provided.
// GetEmployee action
public HttpResponseMessage GetEmployee(int id)
{
Employee emp = EmployeeContext.Employees.FirstOrDefault(e => e.Id == id);
if (emp != null)
{
return Request.CreateResponse<Employee>(HttpStatusCode.OK, emp);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee Not Found");
}
}
Now as you can see above, we have used a HttpResponseMessage as the return type for our get method that will now “CreateResponse” that will return the employee data and “HttpStatusCode.OK” if the employee exists for the Id provided and if no such employee exists then it will create a “CreateErrorResponse” and that will be returning the message “Employee Not Found” and “HttpStatusCode.NotFound”.
Using HttpResponseMessage with Try Catch
Now let's further modify the same example by adding try/catch blocks for exception handling.
// GetEmployee action
public HttpResponseMessage GetEmployee(int id)
{
try
{
Employee emp = EmployeeContext.Employees.Where(e => e.Id == id).FirstOrDefault();
if (emp != null)
{
return Request.CreateResponse<Employee>(HttpStatusCode.OK, emp);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee Not Found");
}
}
catch (Exception ex)
{
// Log exception code goes here
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occurred while executing GetEmployee");
}
}
This makes our code more robust by returning a custom error message with “HttpStatusCode.InternalServerError” if an exception occurs.
Summary
This article showed how to use HttpResponseMessage with raw HTTP responses for returning a message/data with “HttpStatusCode” from our WebApi action.

Rana BagadiPosted Apr 3, 2019, 1:24 AM
Can you please explain how we can write an unit test for the above example.
Amit Kumar PalPosted Jul 6, 2018, 2:00 AM
Simple and understandable explanation. good job...
Chris HPosted Apr 18, 2018, 6:21 AM
Hello Request.CreateResponse getting error : Severity Code Description Project File Line Suppression StateError CS0103 The name 'Request' does not exist in project
tupac amaruPosted Jan 17, 2018, 7:40 AM
I apreciate, very good job !
chatay gurPosted May 24, 2017, 4:36 AM
Awesome post, but how to display the "return Request.CreateResponse<Employee>(HttpStatusCode.OK, emp); " in view or catch it on console.log? Please help
Lokesh KPosted Feb 21, 2017, 7:07 AM
Pretty useful and keep posting new topics
siva prasadPosted Jan 21, 2017, 2:37 AM
Good how can post data by using HttpResponseMessage
Anjali KumariPosted Dec 9, 2016, 9:41 PM
Can you explain how to post the details using HttpResponseMessage
Muhammad Aqib ShehzadPosted Nov 15, 2016, 3:18 AM
I have a question dear throwing the return Request.CreateErrorResponse(HttpStatusCode.NotFound, " Employee Not Found"); will return the 404 not found exception as we will se it in the console. i want to know this is the best approach or is there any thing which not throw exception or it will cost us.
Rajesh BoopalanPosted May 13, 2016, 7:55 AM
In our project previously they have used web api for xml report generation without any authentication (anonymously). All the things they have handled through normal asp.net mvc controller and using like " localhost/Abc/WebApi/Controller/Export _ parameters Localhost-server, ABC- Project Folder, WebApi-Folder, Controller-Controller Name, Export - Method. Above are the details to access xml report using URL. But for only one thing they have used Api controller. Like Below, public virtual HttpResponseMessage ManExport(string ABC, string product, string Release, bool includeInheritedData = false) { } How to access this webApi controller in URL? like normal controller? If its normal controller we can access it via actionresult method name but how to access with HTTPResponsemessage in the URL.
gowtham vPosted Feb 18, 2015, 1:49 AM
Simple and Great explanation
Piyush RaghvaniPosted Sep 13, 2014, 1:49 AM
Nice aricle