【问题标题】:Adding Swagger leading Publish failure Asp.NET Core添加 Swagger 导致发布失败 Asp.NET Core
【发布时间】:2021-08-10 17:21:53
【问题描述】:

得到错误:

发布遇到错误。 确保您的应用程序的 Startup.cs 从 ConfigureServices 中调用 AddSwaggerGen 以生成 swagger 文件。

但是startup.cs这里已经满载:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "WeatherForecast API", Version = "v1" });
    });

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseSwagger();

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

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

【问题讨论】:

  • 尝试清理并重建应用程序,然后重新发布应用程序。另外,在我的asp.net 5应用程序中,我使用默认的SwaggerEndPoint如下:app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "APIApp v1"));,然后将应用程序发布到IIS,一切正常,所以你可以尝试使用这个SwaggerEndpoint。

标签: .net asp.net-core swagger publish


【解决方案1】:

您的代码是正确的。 GitHub 上也有同样的问题,看来问题出在您的项目路径上。

查看此评论。 https://github.com/dotnet/AspNetCore.Docs/issues/20330#issuecomment-722461550

【讨论】:

  • 问题还是一样
  • @MURALIDHARDUBEY 你能在 GitHub 或其他地方分享你的代码吗?
猜你喜欢
  • 2018-10-16
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 2015-02-07
  • 2018-12-22
  • 2021-01-20
相关资源
最近更新 更多