AbpZero的Swagger汉化之旅
做汉化主要是为了出一份前后端都能看得懂的在线文档,废话不多说,我们开始准备,
我们要在启动项目的Startup.cs中重定向一下swagger的读取方式
1、在这个类下面,新增一个方法:
1 public static System.Collections.Generic.List<string> GetXmlCommentPath() 2 { 3 var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + @"\xmls\"; 4 if (!Directory.Exists(baseDirectory)) 5 { 6 Directory.CreateDirectory(baseDirectory); 7 } 8 var docs = Directory.GetFiles(baseDirectory, "*.xml", SearchOption.TopDirectoryOnly).ToList(); 9 return docs; 10 }
2、Ctrl+F用关键词AddSwaggerGen找到调用swagger的代码,把下面的代码覆盖上去
1 var xmls = GetXmlCommentPath(); 2 services.AddSwaggerGen(options => 3 { 4 options.SwaggerDoc("v1", new Info { Title = "Hitouch API", Version = "v1" }); 5 options.DocInclusionPredicate((docName, description) => true); 6 options.OperationFilter<FormFileOperationFilter>(); 7 options.AddSecurityDefinition("Bearer", new ApiKeyScheme 8 { 9 Description = "Authorization format : Bearer {token}", 10 Name = "Authorization", 11 In = "header", 12 Type = "apiKey" 13 }); 14 options.AddSecurityDefinition("Abp.TenantId", new ApiKeyScheme 15 { 16 Description = "Abp.TenantId: {Int}", 17 Name = "Abp.TenantId", 18 In = "header", 19 Type = "apiKey" 20 }); 21 foreach (var xml in xmls) 22 { 23 if (File.Exists(xml)) 24 { 25 options.IncludeXmlComments(xml); 26 } 27 } 28 29 //api界面新增authorize按钮 30 });
3、在启动项目的bin\Debug\netcoreapp2.1的文件夹下新建一个名字为xmls的文件夹
4、把接口层的生成文件定向到上面的文件夹
打完收工,美滋滋。