Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Handling 404 Response

$
0
0

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)
        {
            try
            {
                var course = TheRepository.GetCourse(id);
                if (course != null)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(course));
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
 
            }
            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.

Thanks,

Rubes


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>