要解决ASP.NET Swagger API不显示端点的重载方法的问题,可以使用Swagger的特性来指定哪些方法应该被文档化。
以下是一个示例代码,演示如何使用Swagger的特性来文档化重载的方法:
using Swashbuckle.AspNetCore.Annotations;
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
[HttpGet("{id}")]
[SwaggerOperation(OperationId = "GetById")]
[SwaggerResponse(200, "Success", typeof(MyModel))]
public IActionResult GetById(int id)
{
// 处理逻辑
return Ok();
}
[HttpGet("{id}/details")]
[SwaggerOperation(OperationId = "GetDetailsById")]
[SwaggerResponse(200, "Success", typeof(MyModelDetails))]
public IActionResult GetDetailsById(int id)
{
// 处理逻辑
return Ok();
}
}
在上面的示例中,我们使用了SwaggerOperation
特性来指定每个重载方法的操作ID。这样Swagger就能识别出这两个方法是不同的端点,并将它们分别显示在API文档中。
确保你已经在项目中安装了Swashbuckle.AspNetCore
NuGet包,然后启动应用程序并访问Swagger的终结点(通常是/swagger
),你应该能够看到两个重载方法在API文档中显示出来了。