如果API方法出现问题,我该如何在ASP.NET Core 1.0中返回一个HttpStatus代码?
如果该方法应该返回特定的对象类型,当我尝试返回Http状态码时,会出现错误,提示我无法将我的对象转换为状态码。
[HttpPost]
public async Task<SomeObject> Post([FromBody] inputData)
{
// I detect an error and want to return BadRequest HttpStatus
if(inputData == null)
return new HttpStatusCode(400);
// All is well, so return the object
return myObject;
}
Task<IHttpActionResult>
并将返回更改为return Ok(myObject);
。 - Kien Chuthrow new HttpResponseException(HttpStatusCode.NotFound);
- James