3 回答

TA貢獻1752條經驗 獲得超4個贊
您可以按照以下說明更正您的兩個功能。
如果您的第一個功能:
[HttpGet("{id}")]
public Visitor Get(string id)
{
var result = _visitorRepository.GetFromDB(id);
if (result != _visitorRepository.GetFromDB(id))
return StatusCode(200); // Somehow make this to return "Visitor" type
if (result != null)
return result; // Somehow make this to return "Visitor" type
else
return StatusCode(408); // Somehow make this to return "Visitor" type
}
如果您想使用第二個功能,請進行以下更改:
[HttpGet("{id}")]
public ActionResult Get(string id)
{
var result = _visitorRepository.GetFromDB(id);
if (result != _visitorRepository.GetFromDB(id))
return Ok();
if (result != null)
return Ok(result); // Return type of ActionResult
else
return BadRequest();
}
ActionResult僅供您參考StatusCode(200)返回類型可能就像
return Ok();

TA貢獻1851條經驗 獲得超4個贊
解決方案:
[HttpGet("{id}")]
public IActionResult Get(string id)
{
if (id == null)
{
return BadRequest();
var result = _visitorRepository.GetFromDB(id);
}
if (result != null)
return Ok(result);
else
return NotFound();
}

TA貢獻1790條經驗 獲得超9個贊
return new HttpStatusCodeResult(HttpStatusCode.OK); // OK = 200
https://forums.asp.net/t/2084457.aspx?How+do+I+return+HttpStatus+codes+in+ASP+NET+Core+1+0+API+
- 3 回答
- 0 關注
- 217 瀏覽