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