I have a Web API project and right my methods always returns HttpResponseMessage.
So, if it works or fails I return:
No errors:
return Request.CreateResponse(HttpStatusCode.OK,"File was processed.");
Any error or fail
return Request.CreateResponse(HttpStatusCode.NoContent, "The file has no content or rows to process.");
When I return an object then I use:
return Request.CreateResponse(HttpStatusCode.OK, user);
I would like to know how can I return to my HTML5 client a better encapsulated respose, so I can return more information about the transaction, etc.
I was thinking on creating a custom class that can encapsulate the HttpResponseMessage but also have more data.
Does anyone have implemented something similar?
HttpStatusCode.NoContent
, it will - surprise - return no content, even if you are passing in a string like in your case above. Can be a surprising bug. Like user BigTone 's answer below. This makes sense too according to the specification httpstatuses.com/204A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body.
– Don Cheadle