I've seen a response being sent two different ways from Web API.
return ResponseMessage(new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
or
return StatusCode(HttpStatusCode.UnsupportedMediaType);
Both end up sending a 415 back to the caller. I've looked at the MSDN documentation for the two result classes but still can't figure out what the difference is or why I would choose one over the other.
Use StatusCodeResult
for easy unit testability.
Example(in xUnit):
var result = Assert.IsType<StatusCodeResult>(valuesController.Blah(data));
Assert.Equal(415, result.StatusCode);
Responding to comment: I would prefer something like below:
public IHttpActionResult Get(int id)
{
if(id == 10)
{
return StatusCode(HttpStatusCode.NotFound);
}
return Ok("Some value");
}
rather than:
public IHttpActionResult Get(int id)
{
if(id == 10)
{
return ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Ok("Some value");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With