asp.net web api2 - WebAPI HttpResponse Message -
for webapi2 action results, httpresponsemessage used 1 of return type , request.createresponse used return message.
request.createresponse handy when want return single instance of model if want return multiple rows “model”, there no overload method request.createresponse or request.createresponse supports it(as far read). if post sample, great
also, have few other queries.
• why have go httpresponsemessage rather iqueryable or model return type? • difference between request.createresponse , request.createresponse , able return single instance of model using both methods. how can choose 1 among two?
you can make return type of action method serializable data. content negotiation , formatters turn returned value http response you.
from action results in web api 2:
other return types
for other return types, web api uses media formatter serialize return value. web api writes serialized value response body. response status code 200 (ok).
public class productscontroller : apicontroller { public ienumerable<product> get() { return getallproductsfromdb(); } }a disadvantage of approach cannot directly return error code, such 404. however, can throw
httpresponseexceptionerror codes. more information, see exception handling in asp.net web api.web api uses accept header in request choose formatter. more information, see content negotiation.
example request:
get http://localhost/api/products http/1.1 user-agent: fiddler host: localhost:24127 accept: application/jsonexample response:
http/1.1 200 ok content-type: application/json; charset=utf-8 server: microsoft-iis/8.0 date: mon, 27 jan 2014 08:53:35 gmt content-length: 56 [{"id":1,"name":"yo-yo","category":"toys","price":6.95}]
Comments
Post a Comment