Swagger使用的時候報錯:Failed to load API definition

NuGet添加Swashbuckle.AspNetCore,在Startup.cs添加和啓用中間件Swaggerhtml

public void ConfigureServices(IServiceCollection services) { #region Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1.0.0", Title = "My Web API", Description = "說明文檔", TermsOfService = "None", Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "My.Web", Email = "My.Web@xxx.com", Url = "https://www.cnblogs.com/Zev_Fung/" } }); }); #endregion } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { #region Swagger app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Web API V1"); }); #endregion }

使用自帶的Kestrel web調試,輸入地址:http://localhost:<port>/swagger,默認跳轉到https://localhost:<port>/swagger/index.htmlweb

 

Swagger提示報錯:json

Failed to load API definition.
Errors
Fetch errorInternal Server Error /swagger/v1/swagger.json

 

打開http://localhost:<port>/swagger/v1/swagger.json,提示錯誤api

An unhandled exception occurred while processing the request. NotSupportedException: Ambiguous HTTP method for action - xxxxx.Controllers.BooksController.Post (xxxxx). Actions require an explicit HttpMethod binding for Swagger 2.0 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry) Stack Query Cookies Headers NotSupportedException: Ambiguous HTTP method for action - xxxxx.Controllers.BooksController.Post (xxxxx). Actions require an explicit HttpMethod binding for Swagger 2.0 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry) System.Linq.Enumerable.ToDictionary<TSource, TKey, TElement>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(string documentName, string host, string basePath, string[] schemes) Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) Show raw exception details

 

 

 或者在控制檯查看輸出錯誤app

 

 

 大概意思是:不支持的異常:操做的HTTP方法不明確,方法須要必須指明請求方式ide

在方法加上 [HttpGet]、[HttpPost] 等,就能夠經過 Swagger UI 查看 APIui

 

 

上面只是其中一種引發的Fetch errorInternal Server Error /swagger/v1/swagger.json緣由,其餘也是經過查看錯誤信息解決
相關文章
相關標籤/搜索