WEBAPI2 official website Study record---exception handling

Source: Internet
Author: User

httpresponseexception

When the Webapi controller throws an uncaught exception, by default, most exceptions are converted to the HTTP response of status Code 500, which is a server-side error.

Httpresonseexception is a special case in which the exception can return any specified HTTP status code, or it can return a specific error message.

1  PublicProduct GetProduct (intID)2 {3PRODUCT item =Repository. Get (ID);4     if(Item = =NULL)5     {6         Throw Newhttpresponseexception (httpstatuscode.notfound);7     }8     returnitem;9 }Ten  One  PublicProduct GetProduct (intID) A { -PRODUCT item =Repository. Get (ID); -     if(Item = =NULL) the     { -         varRESP =Newhttpresponsemessage (Httpstatuscode.notfound) -         { -Content =NewStringcontent (string. Format ("No product with ID = {0}", id)), +Reasonphrase ="Product ID not Found" -         } +         Throw Newhttpresponseexception (resp); A     } at     returnitem; -}
View CodeException Filters

You can customize a exception filter to handle exception information, and when a method in a controller throws an uncaught exception (excluding httpresponseexception), the filter is executed.

Exception Filters implements the System.Web.Http.Filters.IExceptionFilter interface, The simplest way is to implement System.Web.Http.Filters.ExceptionFilterAttribute class to customize the exception filter

Registering a custom exception Filters

    1. On the action
    2. On the controller.
    3. In the global
1 Mode 1:2  Public classProductscontroller:apicontroller3 {4 [Notimplexceptionfilter]5      PublicContact GetContact (intID)6     {7         Throw NewNotImplementedException ("This method was not implemented");8     }9 }Ten  One Mode 2: A [Notimplexceptionfilter] -  Public classProductscontroller:apicontroller - { the     // ... - } -  - Mode 3: + GlobalConfiguration.Configuration.Filters.Add ( -     NewProductstore.notimplexceptionfilterattribute ());
View CodeHttperror

The Httperror object provides a uniform way to return error information

1  PublicHttpresponsemessage GetProduct (intID)2 {3PRODUCT item =Repository. Get (ID);4     if(Item = =NULL)5     {6         varMessage =string. Format ("Product with id = {0} not found", id);7         returnrequest.createerrorresponse (httpstatuscode.notfound, message);8     }9     ElseTen     { One         returnrequest.createresponse (Httpstatuscode.ok, item); A     } -}
View Code

The internal Createerrorresponse creates an instance of Httperror, and then creates a httpresponsemessage that contains httperror. The format of the returned error message is related to the formatter selected by Content-negotiation.

1  PublicProduct GetProduct (intID)2 {3PRODUCT item =Repository. Get (ID);4     if(Item = =NULL)5     {6         varMessage =string. Format ("Product with id = {0} not found", id);7         Throw NewHttpresponseexception (8 request.createerrorresponse (httpstatuscode.notfound, message));9     }Ten     Else One     { A         returnitem; -     } - } the  -  Publichttpresponsemessage postproduct (Product Item) - { -     if(!modelstate.isvalid) +     { -         returnrequest.createerrorresponse (Httpstatuscode.badrequest, modelstate); +     } A  at     //implementation not shown ... -}
View Code

WEBAPI2 official website Study record---exception handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.