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
- On the action
- On the controller.
- 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