Asp.Net Core WebApi使用Swagger分组展示接口

先通过Nuget包管理器安装Swashbuckle.AspNetCore包,然后修改Startup.cs的ConfigureServices方法:

       services.AddSwaggerGen(c =>
            {
                // 添加swagger文档
                c.SwaggerDoc("system", new OpenApiInfo()
                {
                    Title = "系统模块",
                    Version = "v1",
                    Description = "接口说明(多模式管理,右上角切换)"
                });
                c.SwaggerDoc("member", new OpenApiInfo()
                {
                    Title = "会员模块",
                    Version = "v1"
                });
                // 设置要展示的接口
                c.DocInclusionPredicate((docName, apiDes) =>
                {
                    if (!apiDes.TryGetMethodInfo(out MethodInfo method))
                        return false;
                    /* 使用ApiExplorerSettingsAttribute里面的GroupName进行特性标识
                     * DeclaringType只能获取controller上的特性
                     * 我们这里是想以action的特性为主
                     */
                    var actionGroup = method.GetCustomAttributes(true).OfType<ApiExplorerSettingsAttribute>().Select(m => m.GroupName);
                    if (actionGroup.Any())
                    {
                        return actionGroup.Any(v => v == docName);
                    }
                    var controllerGroup = method.DeclaringType.GetCustomAttributes(true).OfType<ApiExplorerSettingsAttribute>().Select(m => m.GroupName);
                    return controllerGroup.Any(v => v == docName);
                });
                // 添加授权
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "请输入带有Bearer开头的Token",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey
                });
                // 认证方式,此方式为全局添加
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        new string[] {}
                    }
                });
                // 添加Xml说明文件
                var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                var xmlFileName = AppDomain.CurrentDomain.FriendlyName + ".xml";
                var xmlFilePath = Path.Combine(baseDirectory, xmlFileName);
                if (File.Exists(xmlFilePath))
                {
                    c.IncludeXmlComments(xmlFilePath);
                }
            });

在Configure方法中进行配置:

            // 添加Swagger接口文档服务
            app.UseSwagger();
            // 启用SwaggerUI样式
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/system/swagger.json", "系统模块");
                c.SwaggerEndpoint("/swagger/member/swagger.json", "会员模块");
            });

在controller或者action上加上ApiExplorerSettings,GroupName根据自己的命名进行分组就可以了

ApiExplorerSettings(GroupName = "分组名称")

启动WebApi后,在路径swagger下查看接口文档

 

posted @ 2022-05-09 13:24  广阔之海  阅读(369)  评论(0编辑  收藏  举报