1 回答
TA贡献1784条经验 获得超9个赞
参考ASP.NET Core Web API 中的控制器操作返回类型
ActionResult<T>类型ASP.NET Core 2.1 引入了
ActionResult<T>Web API 控制器操作的返回类型。它使您能够返回派生自ActionResult或返回特定类型的类型。与 IActionResult 类型相比,ActionResult 具有以下优点:
该
[ProducesResponseType]属性的类型属性可以被排除。例如,[ProducesResponseType(200, Type = typeof(Product))]简化为[ProducesResponseType(200)]。操作的预期返回类型是从Tin 推断出来的ActionResult<T>。隐式转换运营商支持的转换
T和ActionResult到ActionResult<T>。T转换为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 关注
- 332 浏览
添加回答
举报
