Bryan
Bryan

Reputation: 3541

Access Swagger not enabled in Azure with Container Instance

I have a webapi running in Auzure Container Instances with open api enabled. But I can't access swagger?

I can navigate to the controller and see output: http://brajzoredockerapi.northeurope.azurecontainer.io/weatherforecast/ (works)

But i can't reach this page: http://brajzoredockerapi.northeurope.azurecontainer.io/swagger/

How can I use swagger within Auzure Container Instances?

Here is my 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();
        });
    }
}

It works when i start the application locally.

Upvotes: 1

Views: 723

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222722

Adding answer so that it will be useful for others,

You are missing the configuration part in the code, move UseSwagger outside so that it works in other environment

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

Upvotes: 1

Related Questions