前言:

前些天有网友提到了那个界面丑陋的SwaggerUI,让我想起了多年前实现的WebAPI文档未完成的功能点,于是,动手了,便有了本文的内容。

开源地址:https://github.com/cyq1162/Taurus.MVC

 

1、WebAPI 文档集成测试功能(增强说明)

开启WebAPI文档:web.config 或 appsettings.json

设置:"IsStartDoc": true

即可通过/doc访问自动生成的WebAPI文档

1、过滤掉无描述的接口。

文档自动生成的来源来自项目中的Xml文档注释

为了能更好的控制显示的结果,不带注释的类或方法(只收录public),不会被收录显示。

2、参数的显示与执行测试说明

自动和成的参数,来自以下方法的注释:

        /// <summary>
        /// 获取Token
        /// </summary>
        /// 
        /// <param name="un" required="true" value="13488889999">用户名</param>
        /// <param name="pwd" type="header">密码</param>
        /// <param name="upload" type="file">图片上传</param>
        /// <returns>{success:true:msg:"tokenString..."}</returns>
        [HttpGet, Require("un", true, RegexConst.Mobile), Require("pwd")]
        [HttpPost]
        public void GetToken(string un, string pwd)
        {
            //is required. is invalid. 判断 是否:中文
            //CheckFormat("{0}不能为空&{0}格式错误", @"un&用户名&^1[3|4|5|8][0-9]\d{8}$", @"pwd&密码&^[\u0391-\uFFE5]+$");
            //string userName = Query<string>("un");
            //string pwd = Query<string>("pwd");
            if (!string.IsNullOrEmpty(un) && !string.IsNullOrEmpty(pwd))
            {
                byte[] data = System.Text.Encoding.UTF8.GetBytes("Taurus:" + un);
                string base64 = Convert.ToBase64String(data);
                Write(base64, true);
            }
            else
            {
                Write("UserName or Password Error", false);
            }
        }

  

其中:

required 决定是否必填 true 或 false(时可不写)
type 决定显示的文本框类型 header(请求头) file(文件上传) 其它(时可不写)
returns 显示返回的结果。


3、运行测试结果

可修改请求路径、请求参数、请求类型。

点击执行运行结果:

系统会自动收集参数并执行,返回结果及请求头的内容。

 

2、附加<%# JS执行功能语法 %>

 为了实现这个自动测试功能,顺路增加了该语法功能。

在/Views/Doc/detail.html 中,有这样一段html代码:

 <tbody id="ParaView" clearflag="1">
                                                <tr>
                                                    <td width="80px">{0}</td>
                                                    <td>{1}</td>
                                                    <td>
                                                        <%# ${required}?'是':'否'%>
                                                    </td>
                                                    <td><%# '${type}'=='file'?'文件':('${type}'=='header'?'请求头':'${type}')%></td>
                                                    <td>
                                                        <![CDATA[
                                                        <input name="{0}" type="<%# '${type}'=='file'?'file':'text'%>" value="{4}" style="width:90%"  rtype="${type}" <%# ${required}?'required="required"':'false'%> />
                                                        ]]>
                                                    </td>

                                                </tr>
                                            </tbody>

其中 ${索引或属性名} 是之前就有的绑定语法。

为了方便html中对于简单的判断的文字变更,思考之后,把<%# 这里的内容都会被JS引擎执行 %>语法块功能给加上了。

细节说明:

由于模板是xhtml语法要求,对于不符合xml语法的地方,可以用

<![CDATA[ ...]]>  包含起来。

 

结束说明:

1、本次版本更新主要就是以上两点功能。

2、由于net core 下支持 <%# %>语法,引用了Microsoft.ClearScript.dll(这个最低支持是3.1),所以netcore的默认版本调高到3.1(因找到了可替换库Z.Expressions,netcore的版本依赖降回2.0 2020-06-12

posted on 2020-05-07 17:56  路过秋天  阅读(1261)  评论(5编辑  收藏  举报
路过秋天