Page 127 - DCAP312_WEB_TECHNOLOGIES_II
P. 127

Unit 6: Error Handling



                     <Authorization>                                                              Notes
                     <allow roles= “MYDOMAIN\Customers” />
                     <deny users= “*” />
                     </authorization>
                     </system.web>
                     </location>
            6.5 Error Handling in ASP.NET Web API


            Web API is a brand new scaffold that makes it easy to build HTTP military. As such, it provides
            several descriptions that make it simple to send back helpful and enlightening error messages
            in a variety of cases we will go over several of these capabilities.
            Now that’s out of the way, let us take a look at what a typical WebAPI error’s HTTP content
            looks like:
                     {
                      “Message”: “No HTTP resource was found that matches the request
                     URI ‘http: //localhost/Foo’.”,
                      “MessageDetail”: “No type was found that matches the controller
                     named ‘Foo’.”
                     }
            So the error is actually just a collection of key-value pairs that provide information about
            what went wrong. This collection of key-value pairs is then sent back to the client in whatever
            content type it asked for through HTTP content negotiation. In the example above, the content
            is actually JSON.
            But if you had “text/xml” in your request’s Accept header for example, you might get this
            response instead:
                     <Error>
                     <Message>No HTTP resource was found that matches the request URI
                     'http://localhost/Foo'.</Message>
                     <MessageDetail>No type was found that matches the controller named
                     'Foo'.</MessageDetail>
                     < /Error>
            This simple format makes it easy for clients to understand what went wrong or to extract relevant
            error information for error logging or reporting.
            6.5.1 HttpError

            What you just saw above is actually an instance of an HttpError being serialized to the wire by
            Json.NET and DataContractSerializer respectively.
            The HttpError type defines a consistent and extensible way for representing error information
            across the framework. Effectively, it is just a Dictionary<string, object> that provides some helpers
            for creating errors that contain error messages, exceptions, or invalid model states. HttpError
            defines the following public constructors:
                     public HttpError();
                     public HttpError(string message);
                     public HttpError(Exception exception, bool includeErrorDetail);
                     public HttpError(ModelStateDictionary modelState, bool includeErrorDetail);

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