ASP. WebAPI 03 Returns results

Source: Internet
Author: User

Httresponsemessage is returned as a message in Webapi, and in Apicontroller we often speak four classes of data as return values, Void,object (serializable), Ihttpactionresult, Httpmessage,

void and Object

Void: Return status Code 200, no data

Object: Return status code 200, data

Ihttpactionresult

WEBAPI defines a ihttpactionresult interface for us, which we can come from by naming it as the return result of the action execution.

In Webapi, a lot of classes have been defined that implement Ihttpactionresult:

These classes can be broadly divided into: Only the status code, redirection, entity serialization, error messages, 201Created and other categories.

Status code

Codestatusresult: Specify the status code

badrequestresult:400

okresult:200

notfoundresult:404

unauthorizedresult:401

conflictresult:409

internalservererrorresult:500

redirect

Status code 302, and return redirect URL

Redirectresult: by Absolute path

Redirecttorouteresult: by route

WEBAPI provides us with a urlhelp class that enables you to convert a route to an absolute path

Entity serialization

FORMATTEDCONTENTRESULT<T>: Custom serialization mode and status code

Negotiatedcontentresult<t>: Custom status code, matching serialization type based on request header information

Oknegotiatedcontentresult<t>: Status code 200, matching serialization type based on request header information

Jsonresult<t>: 200,json serialization of status codes

Error message

exceptionresult:500, eg: {"Message": "Error occurred. "," Exceptionmessage ":" The method or operation was not implemented. "," Exceptiontype ":" System.NotImplementedException "," StackTrace ": null}

Badrequesterrormessageresult:400,eg: {"Message": "Error"}

Invalidmodelstateresult:400,model binding error, eg: {"Message": "The request is invalid. "," Modelstate ": {" argument ": [" The value is not in the expected range.] "]," implemented ": [" the method or operation was not implemented.] "]}}

These three types are eventually replaced with Httperror.

201 Created

Creatednegotiatedcontentresult<t> and Createdatroutenegotiatedcontentresult<t>

Returns the status of "201 Created".

Apicontroller also defines many ways to return to Ihttpactionresult.

Httpresponsemessage

The httpresponsemessage representation includes the status Code and the HTTP response message. Httpresponsemessage is the final request result throughout the WEBAPI, and natural httpresponsemessag can be used as the return result of the action. We can only care about the status code (STATUSCODE), the contents (content), the header information (Headers) If we just look at the return data.

Where WEBAPI defines a number of httpcontent derived classes for us.

Bytearraycontent

Formurlencodedcontent

Multipartcontent

Multipartformdatacontent

Streamcontent

Stringcontent

Both Void,object,ihttpactionresult will generate Httpresponsemessage.

Only one Executeasync method is defined in the Ihttpactionresult interface

Task<httpresponsemessage> Executeasync (cancellationtoken cancellationtoken);

So ihttpactionresult can easily get httpresponsemessage

Source

Github:https://github.com/barlowdu/webapi (Api_3)

ASP. WebAPI 03 Returns results

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.