1 回答

TA貢獻(xiàn)1784條經(jīng)驗 獲得超9個贊
參考ASP.NET Core Web API 中的控制器操作返回類型
ActionResult<T>
類型ASP.NET Core 2.1 引入了
ActionResult<T>
Web API 控制器操作的返回類型。它使您能夠返回派生自ActionResult
或返回特定類型的類型。與 IActionResult 類型相比,ActionResult 具有以下優(yōu)點:
該
[ProducesResponseType]
屬性的類型屬性可以被排除。例如,[ProducesResponseType(200, Type = typeof(Product))]
簡化為[ProducesResponseType(200)]
。操作的預(yù)期返回類型是從T
in 推斷出來的ActionResult<T>
。隱式轉(zhuǎn)換運營商支持的轉(zhuǎn)換
T
和ActionResult
到ActionResult<T>
。T
轉(zhuǎn)換為ObjectResult
,這意味著return new ObjectResult(T);
簡化為return T;
。
以你的控制器為例
[HttpGet]
public async Task<ActionResult<List<EntryDto>>> GetMany(long id) {
//lets say we wanted to validate id
if(id < 1) {
return BadRequest("id must be greater than zero (0)");
}
var result = await _entryService.GetMany(id);
if(result == null || result.Count == 0) {
return NotFound(); // returns proper response code instead of null
}
return result;
}
- 1 回答
- 0 關(guān)注
- 288 瀏覽
添加回答
舉報