<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="2.5.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="2.5.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="2.5.0" />

在csproj 项目文件中添加这几个引用,这个版本好像好看点(个人眼光)

    

        //在StartUp文件的ConfigureServices 中添加以下代码
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("SCMServices", new Info
                {
                    Version = "V2",
                    Title = "ERP销售服务端",
                    Description = "Api说明以及测试",
                    TermsOfService = "SCMServices"
                });
            });

        services.ConfigureSwaggerGen(c =>
            {
                c.DocumentFilter<EnumDocumentFilter>();
                foreach (var item in XmlCommentsFilePath)
                {
                    c.IncludeXmlComments(item);
                }
            });
      //获取项目下所有程序集对应的xml文件
     static List<string> XmlCommentsFilePath
        {
            get
            {
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                DirectoryInfo d = new DirectoryInfo(basePath);
                FileInfo[] files = d.GetFiles("*.xml");
                var xmls = files.Select(a => Path.Combine(basePath, a.FullName)).ToList();
                return xmls;
            }
        }
   //添加格式化枚举    
   public class EnumDocumentFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
        {
            // add enum descriptions to result models
            foreach (var schemaDictionaryItem in swaggerDoc.Definitions)
            {
                var schema = schemaDictionaryItem.Value;
                foreach (var propertyDictionaryItem in schema.Properties)
                {
                    var property = propertyDictionaryItem.Value;
                    var propertyEnums = property.Enum;
                    if (propertyEnums != null && propertyEnums.Count > 0)
                    {
                        property.Description += DescribeEnum(propertyEnums);
                    }
                }
            }
        }

        private static string DescribeEnum(IEnumerable<object> enums)
        {
            var enumDescriptions = new List<string>();
            Type type = null;
            foreach (var enumOption in enums)
            {
                if (type == null) type = enumOption.GetType();
                enumDescriptions.Add($"{Convert.ChangeType(enumOption, type.GetEnumUnderlyingType())} = {Enum.GetName(type, enumOption)},{GetDescription(type, enumOption)}");
            }

            return $"{Environment.NewLine}{string.Join(Environment.NewLine, enumDescriptions)}";
        }

        public static string GetDescription(Type t, object value)
        {
            foreach (MemberInfo mInfo in t.GetMembers())
            {
                if (mInfo.Name == t.GetEnumName(value))
                {
                    foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                    {
                        if (attr.GetType() == typeof(DescriptionAttribute))
                        {
                            return ((DescriptionAttribute)attr).Description;
                        }
                    }
                }
            }
            return string.Empty;
        }
    }

 

posted on 2022-06-30 11:46  不知勿言  阅读(216)  评论(0编辑  收藏  举报