工作上遇到的問題 , 希望在 controller 上面加說明 , 現在大多 example 都是 .net core 的 如果是 .net core 請參考這裡 搜尋了半天發現這個強國人有類似的 但也是 .net core , 所以自己改下沒想到真能動 XD
首先找到你的 SwaggerConfig
找到這串複製 , 修改類別名稱 CustomizeChineseApiDescriptionExtensions
1 2 //c.DocumentFilter<ApplyDocumentVendorExtensions> c.DocumentFilter<CustomizeChineseApiDescriptionExtensions>
接著加入類別 , 假設你有個 LaDiSaiApiController
, 在你的 List<Tag>
加入需要的 controller 即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class CustomizeChineseApiDescriptionExtensions : IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) { swaggerDoc.tags = new List<Tag> { new Tag { //不要加 Controller 取前面的名稱即可 name = "LaDiSaiApi", //中文說明 description = "喇低賽" } }; //自動加入 //swaggerDoc.tags = XmlSummaryReader.GetTags(); } }
後來覺得手動太麻煩 , 於是寫個陽春版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public static class XmlSummaryReader { public static List<Tag> GetTags() { var result = Assembly.GetExecutingAssembly() .GetTypes() .Where( type => typeof( ApiController ).IsAssignableFrom( type ) ) .SelectMany( type => type.GetMethods( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public ) ) .Where( m => !m.GetCustomAttributes( typeof( System.Runtime.CompilerServices.CompilerGeneratedAttribute ), true ).Any() ) .GroupBy( x => x.DeclaringType.FullName ) .Select( x => new Tag { name = XmlSummaryReader.RemoveLastController( Type.GetType( x.Key ).Name ), description = XmlSummaryReader.GetSummary( Type.GetType( x.Key ) ), } ) .ToList(); return result; } public static string GetSummary( Type type ) { //XDocument xmlDoc = XDocument.Load( type.Assembly.Location + ".xml" ); XDocument xmlDoc = XDocument.Load( HostingEnvironment.MapPath( @"~/bin/YOURXML.xml" ) ); XElement typeNode = xmlDoc.Descendants( "member" ) .FirstOrDefault( x => x.Attribute( "name" ).Value == "T:" + type.FullName ); if( typeNode != null ) { XElement summaryNode = typeNode.Element( "summary" ); if( summaryNode != null ) { return summaryNode.Value.Trim(); } } return string.Empty; } public static string RemoveLastController( string input ) { Regex pattern = new Regex( @"Controller$" ); if( pattern.IsMatch( input ) ) { return pattern.Replace( input, "" ); } else { return input; } } }