ASP.NET Core默认swagger页

vddsk6oq  于 12个月前  发布在  .NET
关注(0)|答案(2)|浏览(222)

我使用的是ASP.NET Core 6和NSwag.AspNetCore v13.20.0。
我应该如何配置app.UseSwaggerUi3();,以使默认的swagger页面在该url https://url/prefix/swagger/index.html而不是此https://url/swagger/index.html中打开?
在斯威格前面加个前缀
请注意,UseSwaggerUi已过时
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;
        }
    }

字符串
配置

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;
        }
    }

l5tcr1uw

l5tcr1uw1#

  • 更新-
    这适用于NSwag。AspNetCore
app.UseOpenApi();
app.UseSwaggerUi3(p => {
    p.Path = "/prefix/swagger";
});

字符串

你可以试试这个:

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

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


在launchsettings.json中,您可能需要更改launchUrl
x1c 0d1x的数据
测试


kcugc4gi

kcugc4gi2#

请访问https://github.com/RSuter/NSwag/wiki/AspNetCore-Middleware#hosting-as-an-iis-virtual-application
如果它不为你工作,联系我。

相关问题