public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(); //this replaces these services.AddMvcCore().AddApiExplorer();
...
services.AddSwaggerGen(options =>
{
// make this automatic ???
options.SwaggerDoc("v1",new Info { Version = "v1",Title = "v1 API",Description = "v1 API Description" });
options.SwaggerDoc("v2",new Info { Version = "v2",Title = "v2 API",Description = "v2 API Description" });
...
options.DocInclusionPredicate((version,desc) =>
{
var versions = desc.CustomAttributes().OfType<ApiVersionAttribute>().SelectMany(attr => attr.Versions).ToArray();
var maps = desc.CustomAttributes().OfType<MapToApiVersionAttribute>().SelectMany(attr => attr.Versions).ToArray();
return versions.Any(v => $"v{v.ToString()}" == version) && (!maps.Any() || maps.Any(v => $"v{v.ToString()}" == version));
});
});
}
此代码按预期工作。但是,可以使对SwaggerDoc
的调用自动化,以使代码更加通用吗?在DocInclusionPredicate
参数的desc
中,可以收集版本。