如何在ASP.NET Core中返回自定义HTTP状态/消息而不返回对象,IActionResult等?
来源 https://www.javaroad.cn/questions/66058
ASP.NET Core APIs in the fast lane with Swagger and Autorest
Adding swagger in ASP.NET Core Web API
ASP.NET Core 1.0 MVC API documentation using Swashbuckle Swagger
一:
[HttpGet] [Produces(typeof(Employee))] [SwaggerResponse(System.Net.HttpStatusCode.OK, Type = typeof(Employee))] public async Task<IActionResult> LoadEmployee(string id) { var employee = await repository.GetById(id); if(employee == null) { return NotFound(); } return Ok(employee); }
二:
public async Task<ObjectResult> LoadEmployee(string id) { var employee = await repository.GetById(id); if(employee == null) { return NotFound(); } return StatusCode((int)HttpStatusCode.Ok, employee); }
三:
Swagger支持 ProducesResponseType 属性,它是MVC Web API Core属性,而不是Swagger . 与 SwaggerResponse 相同 .
[HttpGet] [ProducesResponseType(typeof(List<WareHouseView>), 200)] public async Task<IActionResult> GetWareHouse() { var result= await wareHouseServices.GetListAsync(); if (result == null) { return NotFound(); } return Ok(result); }
浙公网安备 33010602011771号