Search code examples
swaggerasp.net-core-webapi

ASP.NET Core default swagger page


I am using ASP.NET Core 6 and NSwag.AspNetCore v13.20.0.

How should I configure the app.UseSwaggerUi3(); in order the default swagger page opens in that url https://url/prefix/swagger/index.html instead of this https://url/swagger/index.html?

With a prefix before the Swagger.

Note that UseSwaggerUi is obsolete

ConfigureServices

public void ConfigureServices(IServiceCollection services)
    {
        _logger.Info("ConfigureServices starting...");

        try
        {
            services.AddControllers(x =>
            {
                x.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
            })
            .AddNewtonsoftJson(x =>
            {
                x.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
            });

            services.AddTypeSignatureSHA256();

            services.AddHttpClient(nameof(HttpClient)).AddPolicyHandler(GetHttpRetryPolicy());

            services.Configure<IISServerOptions>(options => options.AutomaticAuthentication = false);

            services.AddSwaggerDocument(settings =>
            {
                settings.Title = "xxx Api";
            });
            
            _logger.Info("ConfigureServices ended");
        }
        catch (Exception ex)
        {
            _logger.Error(ex, string.Empty);
            throw;
        }
    }

Configure

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifeTime)
    {
        _logger.Info("Configure starting...");

        try
        {
            RegisterApplicationShutdown(appLifeTime);

            if (env.IsDevelopment())
                app.UseDeveloperExceptionPage();

            app.UseRouting();

            app.UseMiddleware<ExceptionsMiddleware>();
            app.UseMiddleware<HealthChecksUIMiddleware>();
            app.UseAppMetricsPrometheus(new AppMetricsPrometheusSettings { UseSystemUsageCollector = true });
            app.UseMiddleware<AppMetricsRequestsMiddleware>();
            app.UseMiddleware<RequestLoggingMiddleware>();

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

            app.UseOpenApi();
            app.UseSwaggerUi3();

            _logger.Info("Configure ended");
        }
        catch (Exception ex)
        {
            _logger.Error(ex, string.Empty);
            throw;
        }
    }

Solution

  • ---------------Update--------------
    This works for NSwag.AspNetCore

    app.UseOpenApi();
    app.UseSwaggerUi3(p => {
        p.Path = "/prefix/swagger";
    });
    

    --------------Old--------------------

    You could try this:

        app.UseSwagger(c =>
        {
            c.RouteTemplate = "prefix/swagger/{documentname}/swagger.json";
        });
    
        app.UseSwaggerUI(c =>
        {  
            c.RoutePrefix = "prefix/swagger";
        });
    

    And in the launchsettings.json, you may want to change the launchUrl
    enter image description here

    Test
    enter image description here