An error is reported when swagger is used: failed to load API definition

NuGet add Swashbuckle.AspNetCore, add and enable middleware Swagger in Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            #region Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v1.0.0",
                    Title = "My Web API",
                    Description = " Description Document " ,
                    TermsOfService = "None",
                    Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "My.Web", Email = "[email protected]", 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 
        }

Use the built-in Kestrel web debugging, enter the address: http://localhost:<port>/swagger, jump to https://localhost:<port>/swagger/index.html by default

 

Swagger prompts an error:

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

Open http://localhost:<port>/swagger/v1/swagger.json, prompt error

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

 

 

Or check the output error in the console

 

 

Probably means: Unsupported exception: The HTTP method of the operation is not clear, and the method needs to specify the request method

Add [HttpGet], [HttpPost], etc. to the method, you can view the API through Swagger UI

 

 

The above is just one of the causes of Fetch error Internal Server Error /swagger/v1/swagger.json, and the others are also solved by viewing the error message

Similar Posts: