參考地址:http://www.cnblogs.com/daxnet/p/6181366.htmljavascript
http://www.jianshu.com/p/fa5a9b76f3edhtml
在 .net core 中使用 swagger 生成接口文檔跟在 asp.net 中使用方式同樣,但把 swagger 添加到項目中不在生成SwaggerConfig.cs 文件 ,需本身配置。web
我這裏安裝的是VS2017, 固然 VS Code也是能夠的。 Nuget安裝Swagger的命令是:json
Install-Package Swashbuckle.AspNetCore -Pre
注意:Nuget包管理添加時必定要注意選擇的時候 選擇 Swashbuckle.AspNetCore 默認的 Swashbuckle 不支持 asp.net coreapi
基於asp.net core 的中間件機制, Swagger也須要加入到中間件服務的列表中, 這樣才能夠啓用Swagger。在 Startup.cs 中的 ConfigureServices 跟 Configure 方法添加 Swagger 代碼以下:服務器
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //添加Swagger. services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "DemoAPI", Version = "v1" }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); //配置Swagger app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "DemoAPI V1"); }); }
從新編譯後訪問,效果以下:架構
根據須要,找到項目 Properties下的 launchSettings.json 文件,好比修改IIS Express節點下的launchUrl,將其改成下圖中的值,這樣啓動時就重定向到指定的地址app
在Startup.cs 類方法 Configure 中啓用 讀取靜態資源文件,並添加 UseSwaggerUI 讀取js文件asp.net
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); app.UseStaticFiles(); //配置Swagger app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "DemoAPI V1"); //加載漢化的js文件,注意 swagger.js文件屬性必須設置爲「嵌入的資源」。 c.InjectOnCompleteJavaScript("/Scripts/swagger.js"); }); }
js文件源碼:
'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(); });
效果如圖所示:
顯示註釋:項目屬性設置生成xml文件路徑,默認生成在bin目錄
修改Startup以下:
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //添加Swagger. services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" }); //添加xml文件 c.IncludeXmlComments(Path.Combine(Directory.GetCurrentDirectory(), "WebAPI.XML")); }); }
在控制器中添加註釋,效果如圖:
最終效果以下: