Returning custom HTTP status code from WebAPI?

You can return the http status code along with the response object using:

 return Ok(responseObject);
 return BadRequest(responseObject);
 return NotFound(responseObject);

      

But what about you want your own http status code back? 402, 429, 500, 501, etc.

+3


source to share


2 answers


return StatusCode(402, responseObject);
return StatusCode(500, responseObject);

      



+3


source


Response.StatusCode = 500; // HttpStatusCode.
return responseObject;

      

OR



Response.StatusCode = StatusCodes.Status500InternalServerError; // HttpStatusCode.
return responseObject;

      

+2


source







All Articles