问题描述:
基于.Net6.0的WebApi 启动Swagger报错:Failed to load API definition。即无法加载API定义。
解决方法:
分析程序输出日志:
错误信息: ERROR Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware - An unhandled exception has occurred while executing the request.
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Ambiguous HTTP method for action - Test.Controllers.TestController.GetPatientList (Test). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOper
根据上述日志,可以快速定义到哪个控制器,哪个方法或接口存在属性问题,根据上面日志显示,是控制器TestController中方法GetPatientList设置成公有(public)属性了,需要修改为private属性。