基於Swagger 實現 webapi 自動生成在線測試文檔css
Step1 添加NuGet包 Swashbucklehtml
step2 修改SwaggerConfig.csweb
Swasshbuckle 安裝完成以後會在App_Start下建立一個名爲SwaggerConfig.cs的類,把內容替換爲:api
1 using System.Web.Http; 2 using WebActivatorEx; 3 using SqlSugar.WebApi; 4 using Swashbuckle.Application; 5 using WebApi; 6 7 [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] 8 9 namespace SqlSugar.WebApi 10 { 11 /// <summary> 12 /// SwaggerConfig 13 /// </summary> 14 public class SwaggerConfig 15 { 16 /// <summary> 17 /// Register 18 /// </summary> 19 public static void Register() 20 { 21 var thisAssembly = typeof(SwaggerConfig).Assembly; 22 23 GlobalConfiguration.Configuration 24 .EnableSwagger(c => 25 { 26 c.SingleApiVersion("v1", "SqlSugar.WebApi"); 27 c.IncludeXmlComments(GetXmlCommentsPath()); 28 c.OperationFilter<HttpHeaderFilter>(); 29 }) 30 .EnableSwaggerUi(c => 31 { 32 // Use the "InjectStylesheet" option to enrich the UI with one or more additional CSS stylesheets. 33 // The file must be included in your project as an "Embedded Resource", and then the resource's 34 // "Logical Name" is passed to the method as shown below. 35 // 36 //c.InjectStylesheet(containingAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testStyles1.css"); 37 38 // Use the "InjectJavaScript" option to invoke one or more custom JavaScripts after the swagger-ui 39 // has loaded. The file must be included in your project as an "Embedded Resource", and then the resource's 40 // "Logical Name" is passed to the method as shown above. 41 // 42 //c.InjectJavaScript(thisAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testScript1.js"); 43 44 // The swagger-ui renders boolean data types as a dropdown. By default, it provides "true" and "false" 45 // strings as the possible choices. You can use this option to change these to something else, 46 // for example 0 and 1. 47 // 48 //c.BooleanValues(new[] { "0", "1" }); 49 50 // By default, swagger-ui will validate specs against swagger.io's online validator and display the result 51 // in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the 52 // feature entirely. 53 //c.SetValidatorUrl("http://localhost/validator"); 54 //c.DisableValidator(); 55 56 // Use this option to control how the Operation listing is displayed. 57 // It can be set to "None" (default), "List" (shows operations for each resource), 58 // or "Full" (fully expanded: shows operations and their details). 59 // 60 //c.DocExpansion(DocExpansion.List); 61 62 // Use the CustomAsset option to provide your own version of assets used in the swagger-ui. 63 // It's typically used to instruct Swashbuckle to return your version instead of the default 64 // when a request is made for "index.html". As with all custom content, the file must be included 65 // in your project as an "Embedded Resource", and then the resource's "Logical Name" is passed to 66 // the method as shown below. 67 // 68 //c.CustomAsset("index", containingAssembly, "YourWebApiProject.SwaggerExtensions.index.html"); 69 70 // If your API has multiple versions and you've applied the MultipleApiVersions setting 71 // as described above, you can also enable a select box in the swagger-ui, that displays 72 // a discovery URL for each version. This provides a convenient way for users to browse documentation 73 // for different API versions. 74 // 75 //c.EnableDiscoveryUrlSelector(); 76 77 // If your API supports the OAuth2 Implicit flow, and you've described it correctly, according to 78 // the Swagger 2.0 specification, you can enable UI support as shown below. 79 // 80 //c.EnableOAuth2Support("test-client-id", "test-realm", "Swagger UI"); 81 }); 82 } 83 84 private static string GetXmlCommentsPath() 85 { 86 return System.String.Format(@"{0}/App_Data/SqlSugar.WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory); 87 } 88 } 89 }
Step3 建立生成XMLapp
右鍵你的項目→屬性→生成→選中下方的 "XML文檔文件" 而後保存ssh
step4 啓動你的項目ide
訪問地址爲:http://localhost:58192/swagger/測試
擴展:在Swagger中 實現 自定義 HTTP Header ui
在開發移動端 API時經常須要驗證權限,驗證參數放在Http請求頭中是再好不過了。WebAPI配合過濾器驗證權限便可this
首先咱們須要建立一個 IOperationFilter 接口的類。IOperationFilter:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Http.Description; using System.Web.Http.Filters; using Swashbuckle.Swagger; namespace WebApi { public class HttpHeaderFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { if (operation.parameters == null) operation.parameters = new List<Parameter>(); var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline(); //判斷是否添加權限過濾器 var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter); //判斷是否容許匿名方法 var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any(); if (isAuthorized && !allowAnonymous) { operation.parameters.Add(new Parameter { name = "Authorization", @in = "header", description = "Token", required = false, type = "string" }); } } } }
在 SwaggerConfig.cs 的 EnableSwagger 配置匿名方法類添加一行註冊代碼
c.OperationFilter<HttpHeaderFilter>();
接上篇token驗證中的權限過濾 地址:http://www.cnblogs.com/dukang1991/articles/5627584.html
添加權限過濾器
運行 swagger
j