swagger配置
配置swaggerx显示驼峰命名,显示枚举类型:
services.AddControllers().AddJsonOptions(config => { config.JsonSerializerOptions.PropertyNamingPolicy = null; //config.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());//swagger显示枚举类型而不是枚举值 });
包含xml文档,显示枚举类型描述:
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "RainTool.WebAPI", Version = "v1" }); foreach (var item in XmlCommentsFilePath) { c.IncludeXmlComments(item); } c.DocumentFilter<SwaggerEnumFilter>();//显示枚举类型的描述 });
static List<string> XmlCommentsFilePath { get { var basePath = PlatformServices.Default.Application.ApplicationBasePath; DirectoryInfo d = new DirectoryInfo(basePath); FileInfo[] files = d.GetFiles("*.xml"); var xmls = files.Select(a => Path.Combine(basePath, a.FullName)).ToList(); return xmls; } }
配置json显示样式:
services.AddMvc(option => { option.Filters.Add<BaseActionFilterAttribute>(); //全局注册 }).AddNewtonsoftJson(options => { //忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //不使用驼峰样式的key options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; });