1.Programm中添加
builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "BarcodeAPI", Version = "v1" }); c.SwaggerDoc("WMS", new OpenApiInfo { Title = "WMS", Version = "WMS" }); c.DocInclusionPredicate((docName, apiDesc) => { if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false; var groupName = methodInfo.DeclaringType.GetCustomAttribute<ApiGroupAttribute>()?.GroupName ?? "v1"; return groupName == docName; }); });
if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "BarcodeAPI v1"); c.SwaggerEndpoint("/swagger/WMS/swagger.json", "WMS"); }); }
2.增加一个特性
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class ApiGroupAttribute : Attribute { public string GroupName { get; } public ApiGroupAttribute(string groupName) { GroupName = groupName; } }
未添加特性的默认为v1
标签:core,v1,WMS,app,groupName,net,swagger,public From: https://www.cnblogs.com/alannxu/p/18685767