c# webapi结合swagger的使用
程序员文章站
2022-06-19 20:04:26
一、使用nuget下载swagger包 Install-Package Swashbuckle 二、配置swagger 1. 安装完Swashbuckle后,nuget会将相关引用添加至WebApi项目,其中会在App_Start文件夹下添加一个SwaggerConfig.cs文件,如下图所示: 2 ......
一、使用nuget下载swagger包
install-package swashbuckle
二、配置swagger
1. 安装完swashbuckle后,nuget会将相关引用添加至webapi项目,其中会在app_start文件夹下添加一个swaggerconfig.cs文件,如下图所示:
2. 设置webapi生成时,顺便生成一份xml文档信息,用于swagger使用
3. 配置swagger引用项目类库生成的xml文档,在swaggerconfig.cs文件里面进行配置
4. 汉化swaggerui
'use strict'; /** * translator for documentation pages. * * to enable translation you should include one of language-files in your index.html * after <script src='lang/translator.js' type='text/javascript'></script>. * for example - <script src='lang/ru.js' type='text/javascript'></script> * * if you wish to translate some new texsts you should do two things: * 1. add a new phrase pair ("new phrase": "new translation") into your language file (for example lang/ru.js). it will be great if you add it in other language files too. * 2. mark that text it templates this way <anyhtmltag data-sw-translate>new phrase</anyhtmltag> or <anyhtmltag data-sw-translate value='new phrase'/>. * the main thing here is attribute data-sw-translate. only inner html, title-attribute and value-attribute are going to translate. * */ window.swaggertranslator = { _words: [], translate: function () { var $this = this; $('[data-sw-translate]').each(function () { $(this).html($this._trytranslate($(this).html())); $(this).val($this._trytranslate($(this).val())); $(this).attr('title', $this._trytranslate($(this).attr('title'))); }); }, _trytranslate: function (word) { return this._words[$.trim(word)] !== undefined ? this._words[$.trim(word)] : word; }, learn: function (wordsmap) { this._words = wordsmap; } }; /* jshint quotmark: double */ window.swaggertranslator.learn({ "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": "从路径", "server returned": "服务器返回" }); $(function () { window.swaggertranslator.translate(); });
注意修改汉化的js文件属性的“生成操作”改为:“嵌入的资源
配置swagger注入汉化文件
三、配置swagger代码
/// <summary> /// swagger文档配置信息 /// </summary> public class swaggerconfig { /// <summary> /// 注册swagger相关配置信息 /// </summary> public static void register() { var thisassembly = typeof(swaggerconfig).assembly; globalconfiguration.configuration .enableswagger(c => { //设置swagger显示的版本号和标题 c.singleapiversion("v1", "api描述文档"); //设置接口描述xml路径地址 var webapixmlpath = string.format("{0}/bin/clearsite.webapi.xml", system.appdomain.currentdomain.basedirectory); c.includexmlcomments(webapixmlpath); //设置接口描述xml路径地址 var applicationxmlpath = string.format("{0}/bin/clearsite.application.xml", system.appdomain.currentdomain.basedirectory); c.includexmlcomments(applicationxmlpath); //设置接口描述xml路径地址 var corexmlpath = string.format("{0}/bin/clearsite.core.xml", system.appdomain.currentdomain.basedirectory); c.includexmlcomments(corexmlpath); //添加操作选项 //c.operationfilter<authtokenheaderparameter>(); }) .enableswaggerui(c => { //加载汉化的js文件,注意 swagger_lang_cn.js 文件属性必须设置为“嵌入的资源”。 c.injectjavascript(system.reflection.assembly.getexecutingassembly(), "clearsite.webapi.scripts.swagger_lang_cn.js"); }); } }
四、运行结果
上一篇: 枸杞和茶叶一起泡水喝吗?