关于.NET CORE 的 swagger 分组简单使用

在避免swagger接口过多时,可以使用分组进行功能区分

简单三部曲

1、添加分组和级别名称

    services.AddSwaggerGen(c =>
            {
                #region swagger分组
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "公共模块",
                    Description = "接口说明(多模式管理,右上角切换)",
                });
                c.SwaggerDoc("khgl", new OpenApiInfo { Title = "客户管理", Version = "khgl" });
                c.SwaggerDoc("htgl", new OpenApiInfo { Title = "合同管理", Version = "htgl" });
                c.SwaggerDoc("yygl", new OpenApiInfo { Title = "运营管理", Version = "yygl" });
                c.SwaggerDoc("jxgl", new OpenApiInfo { Title = "绩效管理", Version = "jxgl" });
                c.SwaggerDoc("kqgl", new OpenApiInfo { Title = "考勤管理", Version = "kqgl" });
                c.SwaggerDoc("ldjsc", new OpenApiInfo { Title = "领导驾驶舱", Version = "ldjsc" });
                 c.SwaggerDoc("xtgl", new OpenApiInfo { Title = "系统管理", Version = "xtgl" });
                c.SwaggerDoc("sbgl", new OpenApiInfo { Title = "设备管理", Version = "sbgl" });
                c.SwaggerDoc("excel", new OpenApiInfo { Title = "文件下载", Version = "excel" });
                //设置要展示的接口
                c.DocInclusionPredicate((docName, apiDes) =>
                {
                    if (!apiDes.TryGetMethodInfo(out MethodInfo method))
                        return false;
                /*使用ApiExplorerSettingsAttribute里面的GroupName进行特性标识
                      * DeclaringType只能获取controller上的特性
                      * 我们这里是想以action的特性为主
                      * */
                    var version = method.DeclaringType.GetCustomAttributes(true).OfType<ApiExplorerSettingsAttribute>().Select(m => m.GroupName);
                    if (docName == "v1" && !version.Any())
                        return true;
                //这里获取action的特性
                var actionVersion = method.GetCustomAttributes(true).OfType<ApiExplorerSettingsAttribute>().Select(m => m.GroupName);
                    if (actionVersion.Any())
                        return actionVersion.Any(v => v == docName);
                    return version.Any(v => v == docName);
                });

               

                #endregion

2、

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
              //以下为关键代码
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "公共模块");
                    c.SwaggerEndpoint("/swagger/khgl/swagger.json", "客户管理");
                    c.SwaggerEndpoint("/swagger/htgl/swagger.json", "合同管理");
                    c.SwaggerEndpoint("/swagger/yygl/swagger.json", "运营管理");
                    c.SwaggerEndpoint("/swagger/jxgl/swagger.json", "绩效管理");
                    c.SwaggerEndpoint("/swagger/kqgl/swagger.json", "考勤管理");
                    c.SwaggerEndpoint("/swagger/ldjsc/swagger.json", "领导驾驶舱");
                    c.SwaggerEndpoint("/swagger/xtsz/swagger.json", "系统设置");
                    c.SwaggerEndpoint("/swagger/sbgl/swagger.json", "设备管理");
                    c.SwaggerEndpoint("/swagger/excel/swagger.json", "文件下载");
                    
                    c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
                });
            }

 3、控制器使用

[Route("api/[controller]/[action]")]
   
//使用
    [ApiExplorerSettings(GroupName = "jxgl")]

 

上一篇:记一次本地正常上线接口报404


下一篇:修改网站颜色为黑白 (100% 灰度)/全页置灰