Page 128 - DCAP312_WEB_TECHNOLOGIES_II
P. 128

Web Technologies-II



                   Notes         6.5.2 HttpResponseException
                                 Now you may be wondering: how do we return an error if the action returns a different
                                 type instead of HttpResponseMessage? This is where HttpResponseException comes in.
                                 HttpResponseException is an exception type defined for WebAPI that serves two purposes:
                                 It allows you to return a specific HTTP response from actions with return types that are not
                                 HttpResponseMessage.

                                 It allows you to short-circuit the WebAPI pipeline pretty much anywhere and return a response
                                 immediately.

                                 Technically, HttpResponseException can be used for any kind of HTTP response, but it is
                                 especially useful in error cases. It allows us to implement actions like this:
                                           public Person Get(int id)
                                           {
                                           if (!_contacts.ContainsKey(id))
                                           {
                                           throw  new  HttpResponseException(Request.CreateErrorResponse(Htt
                                           pStatusCode.NotFound, String.Format( “Contact {0} not found.”, id)));
                                           }
                                           return _contacts[id];
                                           }





                                            Write the code to http error handling.

                                 6.5.3 Error Detail
                                 You may have noticed the “includeErrorDetail” parameter earlier in this post on the HttpError
                                 constructor. This is because typically, servers want to send different error information depending
                                 on whether the client is just consuming the service or whether the client needs additional
                                 debugging information to understand what went wrong on the server. By default, WebAPI will
                                 not send error details to remote clients and will provide these additional error details to clients
                                 on the local machine. Returning to our first example, whereas a local client would see this:
                                           {
                                            “Message”: “No HTTP resource was found that matches the request
                                           URI ‘http: //localhost/Foo’.”,
                                            “Message Detail”: “No type was found that matches the controller
                                           named ‘Foo’.”
                                           }
                                           A remote client would only see this:
                                           {
                                            “Message”: “No HTTP resource was found that matches the request
                                           URI ‘http: //localhost/Foo’.”
                                           }

                                 The difference between Message and Message Detail above is that MessageDetail contains error
                                 information that is WebAPI-specific that remote clients should not have to see in most cases.
                                 The exact meaning of error detail depends on the case. For exceptions, error detail includes the


        122                               LOVELY PROFESSIONAL UNIVERSITY
   123   124   125   126   127   128   129   130   131   132   133