When Swagger used error: Failed to load API definition

NuGet add Swashbuckle.AspNetCore, adding Startup.cs and enabled middleware Swagger

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 = "[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
        }

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

 

Swagger Tip error:

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

 

Open http: // localhost: <port> /swagger/v1/swagger.json, an 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 see the console output error

 

 

 Probably it means: do not support exceptions: HTTP method of operation is not clear, the method needs to be specified in the request mode

In the method of adding [HttpGet], [HttpPost] and the like, you can view the UI API by Swagger

 

Guess you like

Origin www.cnblogs.com/Zev_Fung/p/11588515.html