Pradeep
Pradeep

Reputation: 5530

How to load the swagger page by default whenever browse the Azure API URL/Endpoint

I have created an .Net Core Web API application and then enabled the swagger definition for this API application.

This is the Startup.cs file

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)
    {
        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1",
                new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Demo Web API", Version = "v1" });
        });
        //services.AddControllers();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

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

        app.UseHttpsRedirection();
        app.UseMvc();

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui(HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo Web API");
            //options.RoutePrefix = string.Empty;
            //options.DocumentTitle = "Demo Web API";
            //options.DocExpansion(DocExpansion.List);
        });

    }
}

I have published this application into Azure API App. After that I’m trying to browse the Azure API App default URL then it’s not loading the swagger page as default one. If I want to see the Swagger UI then I need manually append “/swagger” at the end of Azure API App URL.

I need to add the API App as a target into the backend pool of Azure Application Gateway. Whenever I use public IP or DNS name of the Azure Application Gateway then it should display the Swagger UI of the Azure API App.

Upvotes: 1

Views: 1313

Answers (1)

Pradeep
Pradeep

Reputation: 5530

I have updated the Configure() method in Startup.cs file to load the swagger page by default based on the environment.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        if (env.IsDevelopment())
        {
            app.UseSwaggerUI();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // Enable middleware to serve swagger-ui(HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo Web API");
                options.RoutePrefix = string.Empty;
                //options.DocumentTitle = "Demo Web API";
                //options.DocExpansion(DocExpansion.List);
            });
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }

Upvotes: 1

Related Questions