从WebAPI返回自定义HTTP状态代码?

时间:2017-03-30 01:48:05

标签: c# asp.net-core .net-core http-status-codes

您可以使用以下命令返回http状态代码和响应对象:

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

但是你想要返回自己的自定义http状态代码呢? 402,429,500,501等

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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

OR

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