ASP.NET Core默认swagger页

vddsk6oq  于 2024-01-08  发布在  .NET
关注(0)|答案(2)|浏览(343)

我使用的是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

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. _logger.Info("ConfigureServices starting...");
  4. try
  5. {
  6. services.AddControllers(x =>
  7. {
  8. x.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
  9. })
  10. .AddNewtonsoftJson(x =>
  11. {
  12. x.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
  13. });
  14. services.AddTypeSignatureSHA256();
  15. services.AddHttpClient(nameof(HttpClient)).AddPolicyHandler(GetHttpRetryPolicy());
  16. services.Configure<IISServerOptions>(options => options.AutomaticAuthentication = false);
  17. services.AddSwaggerDocument(settings =>
  18. {
  19. settings.Title = "xxx Api";
  20. });
  21. _logger.Info("ConfigureServices ended");
  22. }
  23. catch (Exception ex)
  24. {
  25. _logger.Error(ex, string.Empty);
  26. throw;
  27. }
  28. }

字符串
配置

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifeTime)
  2. {
  3. _logger.Info("Configure starting...");
  4. try
  5. {
  6. RegisterApplicationShutdown(appLifeTime);
  7. if (env.IsDevelopment())
  8. app.UseDeveloperExceptionPage();
  9. app.UseRouting();
  10. app.UseMiddleware<ExceptionsMiddleware>();
  11. app.UseMiddleware<HealthChecksUIMiddleware>();
  12. app.UseAppMetricsPrometheus(new AppMetricsPrometheusSettings { UseSystemUsageCollector = true });
  13. app.UseMiddleware<AppMetricsRequestsMiddleware>();
  14. app.UseMiddleware<RequestLoggingMiddleware>();
  15. app.UseEndpoints(endpoints =>
  16. {
  17. endpoints.MapControllers();
  18. });
  19. app.UseOpenApi();
  20. app.UseSwaggerUi3();
  21. _logger.Info("Configure ended");
  22. }
  23. catch (Exception ex)
  24. {
  25. _logger.Error(ex, string.Empty);
  26. throw;
  27. }
  28. }

l5tcr1uw

l5tcr1uw1#

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

字符串

你可以试试这个:

  1. app.UseSwagger(c =>
  2. {
  3. c.RouteTemplate = "prefix/swagger/{documentname}/swagger.json";
  4. });
  5. app.UseSwaggerUI(c =>
  6. {
  7. c.RoutePrefix = "prefix/swagger";
  8. });


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


展开查看全部
kcugc4gi

kcugc4gi2#

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

相关问题