Httpresponsemessage and Httpresponseexception

Source: Internet
Author: User

The Web API provides httpresponsemessage and httpresponseexception for handling return messages, Httpresponsemessage is used to return a request result message from the client, which you can use Httpresponsemessage the contents of the custom return, httpresponseexception is used to return the client error message when an exception occurs, such as a 404 or 500 error.

In fact, with Httpresponsemessage can also return 404, 500 and other errors, why do you need to use httpresponseexception to return errors? Refer to this article for a point of view, the article mentions that when the call to the Web API service has a different error than expected, it should be necessary to abort the program return error message, the return of the error should use Httpresponseexception, and the use of Httpresponsemessage means that when a client sends a work request and the Web API completes the work correctly, it can use Httpresponsemessage to return a 201 message, so Httpresponsemessage and Httpresponseexception in the use of the fundamental goal is different, with Httpresponsemessage to return an exception error will make the program structure is difficult to identify and not clear, then let us look at Httpresponsemessage and Httpresponseexception mode of operation.

Httpresponsemessage

Httpresonsemessage is used to respond to messages and contain status codes and data content, such as the need to return an instance of Httpresonsemessage can use the extension function of Request Createresponse method, as follows

 Public Httpresponsemessage Deleteproductbyid (int  ID) {// do something ... return Request.createresponse (Httpstatuscode.ok);}

Of course, you can also define the response status code and data content, as follows

 Public Httpresponsemessage Deleteproductbyid (int  ID) {    // do something    ... var response = request.createresponse (httpstatuscode.ok);     = Httpstatuscode.ok;     New Stringcontent ("Delete success! " );         // Response Content    return response;}

In addition, the Createresponse extension method also provides a response method for createresponse<t> generics, as follows

 PublicHttpresponsemessage Getproductbyid (intID) {IEnumerable<product> products =NewProductdao ().    GetProducts (); varProduct = products. Where (p = = P.id = =ID); if(Product. Firstordefault<product> ()! =NULL)        returnRequest.createresponse<product> (Httpstatuscode.ok, Product. First<product>()); Else        Throw Newhttpresponseexception (httpstatuscode.notfound);}

Httpresponseexception

Httpresponseexception to handle exceptions, the specified httpresponsemessage can be returned to the client, and the client does not get a null or error screen when the client calls the Web API error. It is necessary to package the error as a reply message and the most basic case can only reply to the status code, as follows.

 Public httpresponsemessage getallproducts () {    thrownew  httpresponseexception ( Httpstatuscode.notfound);}

Of course, you can define the error message content, as follows

1  PublicHttpresponsemessage Putproduct (intIdstringNamestringCategorystringPriceintstock)2 {3Productdao Productdao =NewProductdao ();4    5         if(productdao.updateproduct (ID, name, category, price, stock))6             returnRequest.createresponse (Httpstatuscode.ok);7         Else8         {9             varResponse =Newhttpresponsemessage (httpstatuscode.internalservererror)Ten             { OneContent =NewStringcontent ("Update Product Error"), AReasonphrase ="Server Error" -             }; -             Throw Newhttpresponseexception (response); the         } -}

Httpresponsemessage and Httpresponseexception (EXT)

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.