Swagger失败,无法加载API定义(还有另一个线程)

时间:2019-01-18 07:46:01

标签: asp.net-core-webapi swagger-ui swagger-2.0 asp.net-core-2.2

我试图向现有的api添加大手笔,在大量失败后,我决定创建一个新的解决方案,创建一个新的asp.net core 2.2 API,并根据

添加了大手笔

https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.2&tabs=visual-studio

有关此问题的其他帖子似乎都表明存在多个httpverbs或一个都不存在的情况,默认值controller则不是这种情况。它给了我错误

  

无法加载API定义。

     

获取错误,服务不可用/swagger/v1/swagger.json

api / swagger / v1 / swagger.json中的实际json文件实际上可以正确呈现

我的创业班是

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddSwaggerGen(c =>{
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
        app.UseMvc();
    }
}

我正在使用Swachbuckle.AspNetCore v 4.01。

1 个答案:

答案 0 :(得分:0)

        String SwaggerEndpoint = "/swagger/v1/swagger.json";

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
            SwaggerEndpoint = "/andi/swagger/v1/swagger.json";
        }


        app.UseMvc();

        app.UseSwagger();
        app.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint(SwaggerEndpoint, "My API");
            //options.RoutePrefix = string.Empty;
        });