ASP.NET WebAPI使用Swagger生成测试文档
SwaggerUI是一个简单的Restful API测试和文档工具。简单、标致、易用(官方demo)。通过读取JSON配置显示API .项目自己仅仅也只依赖一些html,css,js静态文件.你可以几乎放在任何Web容器上使用
捣鼓了好久最终效果如下
1、API控制器和action描述
2、测试接口
使用swagger
1.创建webapi项目解决方案
2.引用swagger nuget包
swashbuckle和swagger.NET
删除多余的SwaggerUI文件夹和配置类SwaggerNet
3、添加接口注释
接下来可以访谒 这样并看不到注释
项目属性->勾选生成xml文档文件
改削SwaggerConfig文件
c.IncludeXmlComments(string.Format(@"{0}\App_Data\Api.xml",AppDomain.CurrentDomain.BaseDirectory));
汉化1.SwaggerConfig
js文件需要嵌入到dll
/// <summary>
/// 中文转换
/// </summary>
var SwaggerTranslator = (function () {
//按时执行检测是否转换成中文,最多执行500次 即500*50/1000=25s
var iexcute = 0,
//中文语言包
_words = {
"Warning: Deprecated": "警告:已过时",
"Implementation Notes": "实现备注",
"Response Class": "响应类",
"Status": "状态",
"Parameters": "参数",
"Parameter": "参数",
"Value": "值",
"Description": "描述",
"Parameter Type": "参数类型",
"Data Type": "数据类型",
"Response Messages": "响应动静",
"HTTP Status Code": "HTTP状态码",
"Reason": "原因",
"Response Model": "响应模型",
"Request URL": "请求URL",
"Response Body": "响应体",
"Response Code": "响应码",
"Response Headers": "响应头",
"Hide Response": "隐藏响应",
"Headers": "头",
"Try it out!": "试一下!",
"Show/Hide": "显示/隐藏",
"List Operations": "显示操纵",
"Expand Operations": "展开操纵",
"Raw": "原始",
"can‘t parse JSON. Raw result": "无法解析JSON. 原始功效",
"Model Schema": "模型架构",
"Model": "模型",
"apply": "应用",
"Username": "用户名",
"Password": "暗码",
"Terms of service": "处事条款",
"Created by": "创建者",
"See more at": "检察更多:",
"Contact the developer": "联系开发者",
"api version": "api版本",
"Response Content Type": "响应Content Type",
"fetching resource": "正在获取资源",
"fetching resource list": "正在获取资源列表",
"Explore": "浏览",
"Show Swagger Petstore Example Apis": "显示 Swagger Petstore 示例 Apis",
"Can‘t read from server. It may not have the appropriate access-control-origin settings.": "无法从处事器读取。可能没有正确设置access-control-origin。",
"Please specify the protocol for": "请指定协议:",
"Can‘t read swagger JSON from": "无法读取swagger JSON于",
"Finished Loading Resource Information. Rendering Swagger UI": "已加载资源信息。正在衬着Swagger UI",
"Unable to read api": "无法读取api",
"from path": "从路径",
"Click to set as parameter value": "点击设置参数",
"server returned": "处事器返回"
},
//按时执行转换
_translator2Cn = function () {
if ($("#resources_container .resource").length > 0) {
_tryTranslate();
}
if ($("#explore").text() == "Explore" && iexcute < 500) {
iexcute++;
setTimeout(_translator2Cn, 50);
}
},