gpt4 book ai didi

c# - 使用容器实例在 Azure 中未启用访问 Swagger

转载 作者:行者123 更新时间:2023-12-03 02:27:32 25 4
gpt4 key购买 nike

我有一个 webapi 在 Auzure 容器实例中运行,并启用了开放 API。但我无法访问 swagger?

我可以导航到 Controller 并查看输出:http://brajzoredockerapi.northeurope.azurecontainer.io/weatherforecast/ (作品)

但我无法访问此页面: http://brajzoredockerapi.northeurope.azurecontainer.io/swagger/

如何在 Auzure 容器实例中使用 swagger?

这是我的 Startup.cs:

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

public IConfiguration Configuration { get; }

// 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 = "WebApplication2", 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("/swagger/v1/swagger.json", "WebApplication2 v1"));
}

app.UseRouting();

app.UseAuthorization();

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

当我在本地启动应用程序时它起作用。

最佳答案

添加答案,以便对其他人有用,

您缺少代码中的配置部分,请将 UseSwagger 移到外部,以便它可以在其他环境中工作

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication2 v1"));

关于c# - 使用容器实例在 Azure 中未启用访问 Swagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66365603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com