Last post Jan 10, 2014 02:43 PM by Rubes
Jan 10, 2014 11:06 AM|Rubes|LINK
Hi Folks, I'm fairly new to Web APIs and I have read the best practice on a Get is to return a 404 HTTP Response when nothing is found. Something like this code I found here (http://bitoftech.net/2013/11/25/implement-model-factory-dependency-injection-configuring-formatters-web-api/):
public HttpResponseMessage GetCourse(int id)
var course = TheRepository.GetCourse(id);
if (course != null)
return Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(course));
catch (Exception ex)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
If the web API is down for some reason, I will also get a 404 returned. In the consuming application, how should I handle these 404 errors? In one instance I'd like to let users know a course is not found, but if the service is down, I will redirect them
to a generic error page. I saw some examples of people modifying the ReasonPhrase. Is that what I should do inside the web API and then check that value on the consuming application? I'm really looking to find out what the best practices are from the consuming
application side of things. I did do a lot of searching, but any URLs to help me out would be great as well.
Jan 10, 2014 12:21 PM|PatriceSc|LINK
Which case have you seen where a "down web API" returns 404 ? 4xx is when the server tells to the client he's doing something wrong (such as trying to reach a resource that doesn't exists). Else it should be a 5xx error if the issue is that the server itself
is not able to service the request because of an issue on his own side.
This is already what you seems to do though I would use
InternalServerError rather than BadRequest (which is rather when the server doesn't understand the query).
Jan 10, 2014 02:43 PM|Rubes|LINK
Thanks for the reply. I realized as I was reading this that my test was invalid. I tested this the lazy way and made an invalid name for the service in the consuming app rather than actually turning the service off. My concern with the 404 should really
never happen unless the service name is changed or I have a wrong URL. I can live with that.
Thanks again for the reply - jogged my memory after a long week.