0

I have configured Swagger in my .NET 6 Web API project. In local, I can access Swagger file. Below is my code for that.

 public static void ConfigureSwaggerMiddleware(this WebApplication app)
        {
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.yaml", "API V1");
                   
                });
            }
            else
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("swagger/v1/swagger.yaml", "API V1");
                    c.RoutePrefix = "api/abc";

                });
            }
        }

Now, I want to configure its URL in dev, test and higher environment like https://devurl/api/abc/swagger.yaml. I am trying above code but I am getting an error as

No webpage was found for the web address: https://localhost:7120/swagger/index.html

niler
  • 23
  • 6

1 Answers1

0

I'm afraid what you want can't be realized if you only changed the route prefix.

public static IApplicationBuilder UseSwaggerUI(
            this IApplicationBuilder app,
            Action<SwaggerUIOptions> setupAction = null)
        {
            SwaggerUIOptions options;
            using (var scope = app.ApplicationServices.CreateScope())
            {
                options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<SwaggerUIOptions>>().Value;
                setupAction?.Invoke(options);
            }

            // To simplify the common case, use a default that will work with the SwaggerMiddleware defaults
            if (options.ConfigObject.Urls == null)
            {
                var hostingEnv = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
                options.ConfigObject.Urls = new[] { new UrlDescriptor { Name = $"{hostingEnv.ApplicationName} v1", Url = "v1/swagger.json" } };
            }

            return app.UseSwaggerUI(options);
        }

When we didn't set any config option for use swagger UI, it will set the default URL as v1/swagger.json with the default route prefix public string RoutePrefix { get; set; } = "swagger"; That makes us get the swagger.json file to load the index page. While you changed the route prefix value, it will make your API application failed to find the swagger configuration file, which will make it 404 error.

So we need to change both launchsetting.json and add app.UseSwagger options. I referred to this answer.

Here's my configuration in Program.cs and in launchsetting.json, I changed "launchUrl": "api/abc/swagger".

if (app.Environment.IsDevelopment())
{
    //app.UseSwagger();
    app.UseSwagger(c =>
    {
        c.RouteTemplate = "api/abc/swagger/{documentname}/swagger.json";
    });
    //app.UseSwaggerUI();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/api/abc/swagger/v1/swagger.json", "API V1");
        c.RoutePrefix = "api/abc/swagger";
    });
}

Then whenever the api application is running, it will direct to https://localhost:7212/api/abc/swagger/index.html and show the APIs. Here's my test result.

enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29