来源 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
相同 .