简体   繁体   中英

Return 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 to return your own custom http status code? 402, 429, 500, 501 etc

return StatusCode(402, responseObject);
return StatusCode(500, responseObject);
Response.StatusCode = 500; // HttpStatusCode.
return responseObject;

OR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM