Uniform return format in WEBAPI frame

Source: Internet
Author: User

Directly on the code

1     /// <summary>2     ///message agent processing, which is used to capture these special exception information3     /// </summary>4      Public classCustomerrormessagedelegatinghandler:delegatinghandler5     {6         protected OverrideTaskSendAsync (httprequestmessage request, CancellationToken CancellationToken)7         {8             return Base. SendAsync (Request, CancellationToken). Continuewith9             {TenHttpresponsemessage response =Responsetocompletetask.result; OneHttperror error =NULL; A                 if(Response. Trygetcontentvalue outerror)) -                 { -                     //Custom error Handling the                     //error. Message = "Error occurred with this interface call"; -                 } -                 if(Error! =NULL) -{//This is a return class that I have created +                     varResultmsg =Newresultmsg {StatusCode = (int) Statuscodeenum.httpurleror, Info =error. Messagedetail}; -                     return Newhttpresponsemessage {Content =Newstringcontent (Resultmsg.tojson (), +System.Text.Encoding.GetEncoding ("UTF-8"),"Application/json"), StatusCode =Httpstatuscode.ok}; A                 } at                 Else -                 { -                     returnresponse; -                 } -             }); -         } in}

Then just register the CS file and locate the Global.asax file

1         protected voidApplication_Start ()2         {3 Arearegistration.registerallareas ();4             //filterconfig.registerglobalfilters (globalfilters.filters);5             //routeconfig.registerroutes (routetable.routes);6             //bundleconfig.registerbundles (bundletable.bundles);7 globalconfiguration.configure (webapiconfig.register);8GLOBALCONFIGURATION.CONFIGURATION.FILTERS.ADD (NewErrorHandler ());9}

Finally done, the effect:

1 {2   " StatusCode " 404 , 3   " Info " " The action matching the request could not be found on the controller "Studytask".  ",4   "Data"null5  }

Uniform return format in WEBAPI frame

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.