This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let's see how we can use Web API to refine our responses.
Follow Along
To follow along committing your changes to this course, you'll need to fork the aspnet-fitness-frog-spa repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd aspnet-fitness-frog-spa
git checkout tags/v3.4 -b refining-our-responses
JSON POST Data
If you're following along, here's the message body data for making a POST request against the API:
{
"date": "1/1/2017",
"activityId": 1,
"duration": 4.5,
"intensity": 1,
"exclude": false,
"notes": null
}
Refining Responses Using HttpResponseMessage
One option that we can use to refine our responses, is to use HttpResponseMessage for our return type.
public HttpResponseMessage Get()
{
return _entriesRepository.GetList();
}
Then to create our HttpResponseMessage return value, we can use the Request CreateResponse
helper method.
var entries = _entriesRepository.GetList();
return Request.CreateResponse(HttpStatusCode.OK, entries);
The CreateResponse
method's first parameter gives us a way to specify any of the HTTP status codes defined in the HttpStatusCode enumeration. We can also capture the HttpResponseMessage object into a local variable, which gives us a way to modify any aspect of the response.
var response = Request.CreateResponse(HttpStatusCode.OK, entries);
return response;
For example, now we can manipulate the response's headers using any of the properties or methods exposed by the HttpResponseHeaders class.
var response = Request.CreateResponse(HttpStatusCode.OK, entries);
response.Headers.[IntelliSense popup]
return response;
For more information on the HttpResponseMessage class, see https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.118).aspx.
ApiController HttpActionResult Helper Methods
Here's the complete list of HttpActionResult helper methods that you can use to refine your HTTP responses.
-
BadRequest
—> Returns an HTTP 400 (“Bad Request”) -
Conflict
—> Returns an HTTP 409 (“Conflict”) -
Content
—> Returns Content (which is automatically negotiated or specified by the developer as media type formatter or content type) -
Created
—> Returns an HTTP 201 -
InternalServerError
—> Returns an HTTP 500 (“Internal Server Error”) -
Json
—> Returns an HTTP 200 (“OK”) and provides the content formatted in JSON -
NotFound
—> Returns an HTTP 404 (“Not Found”) -
Ok
—> Returns an HTTP 200 (“OK”) -
Redirect
—> Returns an HTTP 302 (“Found”) -
ResponseMessage
—> Returns the provided HttpResponseMessage -
StatusCode
—> Returns a response with provided HTTP status code and an empty response body -
Unauthorized
—> Returns an HTTP 401 (“Unauthorized”)
Additional Learning
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up