StatusCodeResult or ResponseMessageResult

I saw the answer being sent in two different ways from the Web API.

return ResponseMessage(new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType)); 

or

 return StatusCode(HttpStatusCode.UnsupportedMediaType); 

Both finish sending 415 back to the caller. I have looked at the MSDN documentation for the two result classes, but still cannot understand what the difference is or why I would choose one of them.

+5
source share
1 answer

Use StatusCodeResult for ease of testing.

Example (in xUnit):

 var result = Assert.IsType<StatusCodeResult>(valuesController.Blah(data)); Assert.Equal(415, result.StatusCode); 

Responding to a comment: I would prefer something like below:

 public IHttpActionResult Get(int id) { if(id == 10) { return StatusCode(HttpStatusCode.NotFound); } return Ok("Some value"); } 

but not:

 public IHttpActionResult Get(int id) { if(id == 10) { return ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound)); } return Ok("Some value"); } 
+4
source

All Articles