如何更改swagger路由端点url并保存api版本控制



我有一个启动文件,在那里我配置了SwaggerUI和Enpoints Url

namespace AccountingService
{
    public class Startup
    {
        //public Startup(IConfiguration configuration, IHostEnvironment env, ILogger<Startup> logger) { }
 
       //public void ConfigureServices(IServiceCollection services){ }
 
        public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider, IApiVersionDescriptionProvider provider)
        {
            if (HostEnvironment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseIpRateLimiting();
app.ConfigureCustomMiddleware(_appSettings);
            UserHelper.InitDependencies(serviceProvider.GetRequiredService<IUserDataService>());
            
app.UseRouting();
app.UseCors("MyPolicy");
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseTimezone();
app.UseErrorService(serviceProvider);
app.UseSession();
app.UseUserService();
 
            if (_appSettings.Swagger.Enabled)
            {
                app.UseSwagger();
                app.UseSwaggerUI(options =>
                {
                    foreach (var description in provider.ApiVersionDescriptions)
                    {
                        options.SwaggerEndpoint($"swagger/{description.GroupName}/swagger.json", description.GroupName.ToLowerInvariant());
                        options.RoutePrefix = string.Empty;
                    }
                });
            }
            
            //app.UseNamedSwagger(_appSettings.ServiceName, _appSettings.ServiceTitle, _appSettings.Version);
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

在这种情况下,它是如何在swagger url generic中工作的本地主机(港口):/得意/{groupname}/swagger.json

我需要在这里更改swagger url为例如[name-service]/得意/v1/swagger.json它需要在nginx上更正路由,我们的服务器在docker上compose

当我尝试更改SwaggerEndpoint任何其他url值时,如

_appSettings.ServiceName
if (_appSettings.Swagger.Enabled)
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"{_appSettings.ServiceName}/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
options.RoutePrefix = string.Empty;
}
});
}

它完全坏了,与那个无关。

同样的问题,当我试图改变只有路由前缀结果

app.RoutePrefix = $"{_appSettings.ServiceName}/swagger";

我不知道如何正确地组织问题和问题格。

但是当我使用其他方法UseNamedSwagger

app.UseNamedSwagger(_appSettings.ServiceName, _appSettings.ServiceTitle, _appSettings.Version);

不是

app.UseSwaggerUI(options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"{_appSettings.ServiceName}/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
options.RoutePrefix = string.Empty;
}
});

它工作正常,并且swagger url更改正确,但这里我的版本控制消失v1,v2但是这里我的版本控制消失了v1 v2

在这里添加我如何构建API控制器

[Authorize]
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ActsController : ControllerBase

我需要改变或添加什么来正确显示Swagger url " edu -service/Swagger/v1/Swagger "并没有丢失我的版本控制定义(v1,v2)

ConfigureServices方法

public void ConfigureServices(IServiceCollection services)
{
_logger.LogTrace("Startup::ConfigureServices");
//Enable Cors
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
// configure strongly typed settings objects
_appSettings = Configuration.GetSection("AppSettings").Get<AppSettings>();
services.Configure<AppSettings>(Configuration.GetSection(nameof(AppSettings)));
// configure jwt authentication
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
// configure swagger settings
var swaggerSettings = Configuration.GetSection("SwaggerAdminSettings").Get<SwaggerSettingsModel>();
services.Configure<ErrorServiceApiConfiguration>(
Configuration.GetSection(nameof(ErrorServiceApiConfiguration)));
services.AddAuthentication(..);
// requires using Microsoft.Extensions.Options
services.Configure<FormOptions>(o =>
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = int.MaxValue;
o.MemoryBufferThreshold = int.MaxValue;
});
//Add IMapper profile configuration to DI
var mapperConfig = new MapperConfiguration(mc => mc.AddProfile(new AutoMapperProfile()));
services.AddSingleton(mapperConfig.CreateMapper());
// many other services....
// -- API Rate Limit Configuration --
services.AddMemoryCache();
services.AddInMemoryRateLimiting();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
//Add Services here
//==== CommonServiceTools services ====
services.AddLogger();
services.ConfigureCrud();
services.AddExternalApi();
services.AddWarehouseApi();
services.AddInvoiceManager();
// many other services....
services.AddDistributedMemoryCache();
services.AddSession();
services.Configure<MinioSettings>(Configuration.GetSection(nameof(MinioSettings)));
services.Configure<WarehouseApiSettings>(Configuration.GetSection(nameof(WarehouseApiSettings)));
services.AddControllers(opt =>
{ opt.UseCentralRoutePrefix(new RouteAttribute(_appSettings.ServiceName)); })
.AddNewtonsoftJson(options => options.UseMemberCasing())
.AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())).SetCompatibilityVersion(CompatibilityVersion.Latest);
//API versioning
services.AddApiVersioning(
o =>
{
//o.Conventions.Controller<UserController>().HasApiVersion(1, 0);
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
o.ApiVersionReader = new UrlSegmentApiVersionReader();
}
);
// note: the specified format code will format the version as "'v'major[.minor][-status]"
services.AddVersionedApiExplorer(
options =>
{
options.GroupNameFormat = "'v'VVV";
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
// can also be used to control the format of the API version in route accounting-service
options.SubstituteApiVersionInUrl = true;
});
services.AddWkhtmltopdf();

if (_appSettings.Swagger.Enabled)
{
// -- Swagger configuration -- 
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigurationSwaggerOptions>();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.OperationFilter<SwaggerDefaultValues>();
c.UseAllOfToExtendReferenceSchemas();
//c.SwaggerDoc(_appSettings.Version, 
//    new OpenApiInfo { Title = _appSettings.ServiceTitle, Version = _appSettings.Version });
/*Authorization*/
var jwtSecurityScheme = new OpenApiSecurityScheme
{
Scheme = "bearer",
BearerFormat = "JWT",
Name = "JWT Authentication",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Description = "Put **_ONLY_** your JWT Bearer token on textbox below!",
Reference = new OpenApiReference
{
Id = JwtBearerDefaults.AuthenticationScheme,
Type = ReferenceType.SecurityScheme
}
};
c.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme);
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ jwtSecurityScheme, Array.Empty<string>() }
});
//1-Get all the assemblies of the project to add the related XML Comments
Assembly currentAssembly = Assembly.GetExecutingAssembly();
AssemblyName[] referencedAssemblies = currentAssembly.GetReferencedAssemblies();
IEnumerable<AssemblyName> allAssemblies = null;
if (referencedAssemblies != null && referencedAssemblies.Any())
allAssemblies = referencedAssemblies.Union(new AssemblyName[] { currentAssembly.GetName() });
else
allAssemblies = new AssemblyName[] { currentAssembly.GetName() };
IEnumerable<string> xmlDocs = allAssemblies
.Select(a => Path.Combine(Path.GetDirectoryName(currentAssembly.Location), $"{a.Name}.xml"))
.Where(f => File.Exists(f));
//2-Add the path to the XML comments for the assemblies having enabled the doc generation
if (xmlDocs != null && xmlDocs.Any())
{
foreach (var item in xmlDocs)
{
c.IncludeXmlComments(item, includeControllerXmlComments: true);
}
}
});
}
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
_logger.LogDebug("Startup::Constructor::Settings loaded");
}

解决了,就在上面注释

//c.SwaggerDoc(_appSettings.Version,
//    new OpenApiInfo { Title = _appSettings.ServiceTitle, Version = _appSettings.Version });

做这件事

if (_appSettings.Swagger.Enabled)
{
app.UseSwagger(delegate (SwaggerOptions c)
{
c.RouteTemplate = $"/{_appSettings.ServiceName}/swagger/{{documentName}}/swagger.json";
}).UseSwaggerUI(options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint(
$"/{_appSettings.ServiceName}/swagger/{description.GroupName}/swagger.json",
$"{_appSettings.ServiceTitle} {description.GroupName}");
options.RoutePrefix = $"{_appSettings.ServiceName}/swagger";
}
});
}

相关内容

  • 没有找到相关文章