Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Http.Results.OkResult vs Content(HttpStatusCode.OK)

In Web Api 2 I can return a 200 in two ways.

return Ok() or return Ok("Some value")

Or

return Content(HttpStatusCode.OK, "Some value") this can have any value included, along with formatters and some other parameters.

I know that return Content(...) supports all HttpStatusCodes so it will be used for responses like NoContent where there is no return NoContent()

Is return Ok() just a shortcut for return Content(..)?

like image 498
Bryan Avatar asked Nov 08 '22 00:11

Bryan


1 Answers

Is return Ok() just a shortcut for return Content(..)?

No

If you look at the methods in question you will see what they return IHttpActionResult derived classes

protected internal virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, T value);        
protected internal FormattedContentResult<T> Content<T>(HttpStatusCode statusCode, T value, MediaTypeFormatter formatter);        
protected internal virtual FormattedContentResult<T> Content<T>(HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType);    
protected internal FormattedContentResult<T> Content<T>(HttpStatusCode statusCode, T value, MediaTypeFormatter formatter, string mediaType);    
protected internal virtual OkResult Ok();
protected internal virtual OkNegotiatedContentResult<T> Ok<T>(T content);

Each one of the return types are unrelated to each other except for the fact that they are derived from IHttpActionResult.

like image 52
Nkosi Avatar answered Nov 14 '22 23:11

Nkosi