Swagger使用的奇淫技巧

一、背景


最近重构了公司一个老接口项目,弃用了原来的webservice服务,新项目用webapi写的。所以就有了 《Swagger使用的奇淫技巧》这篇文章。常规使用的技巧我这里就不在多赘述了,很多老鸟写的已经很详细了。这里我主要介绍下怎么使用VS remark 写MarkDown并且使用Swagger显示

二、ToDo


  • 新建一个WEB API项目,项目名称Demo

  • 引入Swagger

Install-Package Swashbuckle -ProjectName Demo -Version 5.6.0
528545-20180112135204754-103325445.gif

  • 右键Demo项目-->生成-->勾选xml文档文件,ctrl+s保存

  • 打开App_Start文件夹,打开SwaggerConfig文件

    添加方法GetXmlCommentsPath,并且打开c.IncludeXmlComments(GetXmlCommentsPath()); 这个配置

      public static string GetXmlCommentsPath()
        {
#if DEBUG
            return AppDomain.CurrentDomain.BaseDirectory + "\\bin\\demo.xml";\
#elif RELEASE
             return AppDomain.CurrentDomain.BaseDirectory + "\\bin\\Release\\demo.xml";

#else
            return AppDomain.CurrentDomain.BaseDirectory + "\\bin\\demo.xml";
#endif
        }
  • 打开App_Start文件夹,新建文件 FormatCommentProperties.cs,并引入Swashbuckle.Swagger 类库 ,并在SwaggerConfig文件中添加新指向XML描述过滤c.OperationFilter();
public class FormatCommentProperties : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            operation.description = Formatted(operation.description);
            operation.summary = Formatted(operation.summary);
        }


        private string Formatted(string text)
        {
            if (text == null) return null;

            // Strip out the whitespace that messes up the markdown in the xml comments,
            // but don't touch the whitespace in <code> blocks. Those get fixed below.
            string resultString = Regex.Replace(text, @"(^[ \t]+)(?![^<]*>|[^>]*<\/)", "", RegexOptions.Multiline);
            resultString = Regex.Replace(resultString, @"<code[^>]*>", "``` C#", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
            resultString = Regex.Replace(resultString, @"</code[^>]*>", "```", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
            resultString = Regex.Replace(resultString, @"<!--", "", RegexOptions.Multiline);
            resultString = Regex.Replace(resultString, @"-->", "", RegexOptions.Multiline);

            try
            {
                string pattern = @"```(.*?)```";

                foreach (Match match in Regex.Matches(resultString, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline))
                {
                    var formattedPreBlock = FormatPreBlock(match.Value);
                    resultString = System.Web.HttpUtility.HtmlDecode(resultString.Replace(match.Value, formattedPreBlock));
                }
                return resultString;
            }
            catch
            {
                // Something went wrong so just return the original resultString
                return resultString;
            }
        }

        private string FormatPreBlock(string preBlock)
        {
            // Split the <pre> block into multiple lines
            var linesArray = preBlock.Split('\n');
            if (linesArray.Length < 2)
            {
                return preBlock;
            }
            else
            {
                // Get the 1st line after the <pre>
                string line = linesArray[1];
                int lineLength = line.Length;
                string formattedLine = line.TrimStart(' ', '\t');
                int paddingLength = lineLength - formattedLine.Length;

                // Remove the padding from all of the lines in the <pre> block
                for (int i = 1; i < linesArray.Length - 1; i++)
                {
                    linesArray[i] = linesArray[i].Substring(paddingLength);
                }

                var formattedPreBlock = string.Join("", linesArray);
                return formattedPreBlock;
            }
        }
    }
  • 写注释
    528545-20180112135128676-909738025.png

  • 现在启动站点
    528545-20180112135145441-1556131954.png

转载于:https://www.cnblogs.com/router/p/8274619.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值