※This article is based on .NET Core2.2
报出下面的错误
Failed to load API definition
为了查看具体的错误信息
https://localhost:端口号/swagger/v1/swagger.json
An unhandled exception occurred while processing the request. SwaggerGeneratorException: Ambiguous HTTP method for action - SINOData.Controllers.OrderController.Success (SINOData). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository) Stack Query Cookies Headers Routing SwaggerGeneratorException: Ambiguous HTTP method for action - SINOData.Controllers.OrderController.Success (SINOData). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(string documentName, string host, string basePath) Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
看报错得知 action HomeController.Index 未明确标记 HttpMethod,这个action可以访问是因为全局路由所以可以访问。看到这个报错直接在报错对应的action上增加HttpMethod即可。
[ApiController] [Route("/")] public class HomeController : Controller { [HttpGet] public ResponseVm Index() { return ResponseVm.Success(); } }
コメント: