SwaggerConfig.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System.Web.Http;
  2. using WebActivatorEx;
  3. using XdCxRhDW.WebApi;
  4. using Swashbuckle.Application;
  5. [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
  6. namespace XdCxRhDW.WebApi
  7. {
  8. /// <summary>
  9. /// 安装Swagger Nuget包时自动生成的文件
  10. /// </summary>
  11. public class SwaggerConfig
  12. {
  13. /// <summary>
  14. /// 配置Swagger
  15. /// </summary>
  16. public static void Register()
  17. {
  18. var thisAssembly = typeof(SwaggerConfig).Assembly;
  19. GlobalConfiguration.Configuration
  20. .EnableSwagger(c =>
  21. {
  22. // By default, the service root url is inferred from the request used to access the docs.
  23. // However, there may be situations (e.g. proxy and load-balanced environments) where this does not
  24. // resolve correctly. You can workaround this by providing your own code to determine the root URL.
  25. //
  26. //c.RootUrl(req => GetRootUrlFromAppConfig());
  27. // If schemes are not explicitly provided in a Swagger 2.0 document, then the scheme used to access
  28. // the docs is taken as the default. If your API supports multiple schemes and you want to be explicit
  29. // about them, you can use the "Schemes" option as shown below.
  30. //
  31. //c.Schemes(new[] { "http", "https" });
  32. // Use "SingleApiVersion" to describe a single version API. Swagger 2.0 includes an "Info" object to
  33. // hold additional metadata for an API. Version and title are required but you can also provide
  34. // additional fields by chaining methods off SingleApiVersion.
  35. //
  36. c.SingleApiVersion("v1", "XdCxRhDW.WebApi");
  37. // If you want the output Swagger docs to be indented properly, enable the "PrettyPrint" option.
  38. //
  39. //c.PrettyPrint();
  40. // If your API has multiple versions, use "MultipleApiVersions" instead of "SingleApiVersion".
  41. // In this case, you must provide a lambda that tells Swashbuckle which actions should be
  42. // included in the docs for a given API version. Like "SingleApiVersion", each call to "Version"
  43. // returns an "Info" builder so you can provide additional metadata per API version.
  44. //
  45. //c.MultipleApiVersions(
  46. // (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
  47. // (vc) =>
  48. // {
  49. // vc.Version("v2", "Swashbuckle Dummy API V2");
  50. // vc.Version("v1", "Swashbuckle Dummy API V1");
  51. // });
  52. // You can use "BasicAuth", "ApiKey" or "OAuth2" options to describe security schemes for the API.
  53. // See https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md for more details.
  54. // NOTE: These only define the schemes and need to be coupled with a corresponding "security" property
  55. // at the document or operation level to indicate which schemes are required for an operation. To do this,
  56. // you'll need to implement a custom IDocumentFilter and/or IOperationFilter to set these properties
  57. // according to your specific authorization implementation
  58. //
  59. //c.BasicAuth("basic")
  60. // .Description("Basic HTTP Authentication");
  61. //
  62. // NOTE: You must also configure 'EnableApiKeySupport' below in the SwaggerUI section
  63. //c.ApiKey("apiKey")
  64. // .Description("API Key Authentication")
  65. // .Name("apiKey")
  66. // .In("header");
  67. //
  68. //c.OAuth2("oauth2")
  69. // .Description("OAuth2 Implicit Grant")
  70. // .Flow("implicit")
  71. // .AuthorizationUrl("http://petstore.swagger.wordnik.com/api/oauth/dialog")
  72. // //.TokenUrl("https://tempuri.org/token")
  73. // .Scopes(scopes =>
  74. // {
  75. // scopes.Add("read", "Read access to protected resources");
  76. // scopes.Add("write", "Write access to protected resources");
  77. // });
  78. // Set this flag to omit descriptions for any actions decorated with the Obsolete attribute
  79. //c.IgnoreObsoleteActions();
  80. // Each operation be assigned one or more tags which are then used by consumers for various reasons.
  81. // For example, the swagger-ui groups operations according to the first tag of each operation.
  82. // By default, this will be controller name but you can use the "GroupActionsBy" option to
  83. // override with any value.
  84. //
  85. //c.GroupActionsBy(apiDesc => apiDesc.HttpMethod.ToString());
  86. // You can also specify a custom sort order for groups (as defined by "GroupActionsBy") to dictate
  87. // the order in which operations are listed. For example, if the default grouping is in place
  88. // (controller name) and you specify a descending alphabetic sort order, then actions from a
  89. // ProductsController will be listed before those from a CustomersController. This is typically
  90. // used to customize the order of groupings in the swagger-ui.
  91. //
  92. //c.OrderActionGroupsBy(new DescendingAlphabeticComparer());
  93. // If you annotate Controllers and API Types with
  94. // Xml comments (http://msdn.microsoft.com/en-us/library/b2s063f7(v=vs.110).aspx), you can incorporate
  95. // those comments into the generated docs and UI. You can enable this by providing the path to one or
  96. // more Xml comment files.
  97. //
  98. //c.IncludeXmlComments(GetXmlCommentsPath());
  99. // Swashbuckle makes a best attempt at generating Swagger compliant JSON schemas for the various types
  100. // exposed in your API. However, there may be occasions when more control of the output is needed.
  101. // This is supported through the "MapType" and "SchemaFilter" options:
  102. //
  103. // Use the "MapType" option to override the Schema generation for a specific type.
  104. // It should be noted that the resulting Schema will be placed "inline" for any applicable Operations.
  105. // While Swagger 2.0 supports inline definitions for "all" Schema types, the swagger-ui tool does not.
  106. // It expects "complex" Schemas to be defined separately and referenced. For this reason, you should only
  107. // use the "MapType" option when the resulting Schema is a primitive or array type. If you need to alter a
  108. // complex Schema, use a Schema filter.
  109. //
  110. //c.MapType<ProductType>(() => new Schema { type = "integer", format = "int32" });
  111. // If you want to post-modify "complex" Schemas once they've been generated, across the board or for a
  112. // specific type, you can wire up one or more Schema filters.
  113. //
  114. //c.SchemaFilter<ApplySchemaVendorExtensions>();
  115. // In a Swagger 2.0 document, complex types are typically declared globally and referenced by unique
  116. // Schema Id. By default, Swashbuckle does NOT use the full type name in Schema Ids. In most cases, this
  117. // works well because it prevents the "implementation detail" of type namespaces from leaking into your
  118. // Swagger docs and UI. However, if you have multiple types in your API with the same class name, you'll
  119. // need to opt out of this behavior to avoid Schema Id conflicts.
  120. //
  121. //c.UseFullTypeNameInSchemaIds();
  122. // Alternatively, you can provide your own custom strategy for inferring SchemaId's for
  123. // describing "complex" types in your API.
  124. //
  125. //c.SchemaId(t => t.FullName.Contains('`') ? t.FullName.Substring(0, t.FullName.IndexOf('`')) : t.FullName);
  126. // Set this flag to omit schema property descriptions for any type properties decorated with the
  127. // Obsolete attribute
  128. //c.IgnoreObsoleteProperties();
  129. // In accordance with the built in JsonSerializer, Swashbuckle will, by default, describe enums as integers.
  130. // You can change the serializer behavior by configuring the StringToEnumConverter globally or for a given
  131. // enum type. Swashbuckle will honor this change out-of-the-box. However, if you use a different
  132. // approach to serialize enums as strings, you can also force Swashbuckle to describe them as strings.
  133. //
  134. //c.DescribeAllEnumsAsStrings();
  135. // Similar to Schema filters, Swashbuckle also supports Operation and Document filters:
  136. //
  137. // Post-modify Operation descriptions once they've been generated by wiring up one or more
  138. // Operation filters.
  139. //
  140. //c.OperationFilter<AddDefaultResponse>();
  141. //
  142. // If you've defined an OAuth2 flow as described above, you could use a custom filter
  143. // to inspect some attribute on each action and infer which (if any) OAuth2 scopes are required
  144. // to execute the operation
  145. //
  146. //c.OperationFilter<AssignOAuth2SecurityRequirements>();
  147. // Post-modify the entire Swagger document by wiring up one or more Document filters.
  148. // This gives full control to modify the final SwaggerDocument. You should have a good understanding of
  149. // the Swagger 2.0 spec. - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md
  150. // before using this option.
  151. //
  152. //c.DocumentFilter<ApplyDocumentVendorExtensions>();
  153. // In contrast to WebApi, Swagger 2.0 does not include the query string component when mapping a URL
  154. // to an action. As a result, Swashbuckle will raise an exception if it encounters multiple actions
  155. // with the same path (sans query string) and HTTP method. You can workaround this by providing a
  156. // custom strategy to pick a winner or merge the descriptions for the purposes of the Swagger docs
  157. //
  158. //c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
  159. // Wrap the default SwaggerGenerator with additional behavior (e.g. caching) or provide an
  160. // alternative implementation for ISwaggerProvider with the CustomProvider option.
  161. //
  162. //c.CustomProvider((defaultProvider) => new CachingSwaggerProvider(defaultProvider));
  163. })
  164. .EnableSwaggerUi(c =>
  165. {
  166. // Use the "DocumentTitle" option to change the Document title.
  167. // Very helpful when you have multiple Swagger pages open, to tell them apart.
  168. //
  169. //c.DocumentTitle("My Swagger UI");
  170. // Use the "InjectStylesheet" option to enrich the UI with one or more additional CSS stylesheets.
  171. // The file must be included in your project as an "Embedded Resource", and then the resource's
  172. // "Logical Name" is passed to the method as shown below.
  173. //
  174. //c.InjectStylesheet(containingAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testStyles1.css");
  175. // Use the "InjectJavaScript" option to invoke one or more custom JavaScripts after the swagger-ui
  176. // has loaded. The file must be included in your project as an "Embedded Resource", and then the resource's
  177. // "Logical Name" is passed to the method as shown above.
  178. //
  179. //c.InjectJavaScript(thisAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testScript1.js");
  180. // The swagger-ui renders boolean data types as a dropdown. By default, it provides "true" and "false"
  181. // strings as the possible choices. You can use this option to change these to something else,
  182. // for example 0 and 1.
  183. //
  184. //c.BooleanValues(new[] { "0", "1" });
  185. // By default, swagger-ui will validate specs against swagger.io's online validator and display the result
  186. // in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the
  187. // feature entirely.
  188. //c.SetValidatorUrl("http://localhost/validator");
  189. //c.DisableValidator();
  190. // Use this option to control how the Operation listing is displayed.
  191. // It can be set to "None" (default), "List" (shows operations for each resource),
  192. // or "Full" (fully expanded: shows operations and their details).
  193. //
  194. //c.DocExpansion(DocExpansion.List);
  195. // Specify which HTTP operations will have the 'Try it out!' option. An empty paramter list disables
  196. // it for all operations.
  197. //
  198. //c.SupportedSubmitMethods("GET", "HEAD");
  199. // Use the CustomAsset option to provide your own version of assets used in the swagger-ui.
  200. // It's typically used to instruct Swashbuckle to return your version instead of the default
  201. // when a request is made for "index.html". As with all custom content, the file must be included
  202. // in your project as an "Embedded Resource", and then the resource's "Logical Name" is passed to
  203. // the method as shown below.
  204. //
  205. //c.CustomAsset("index", containingAssembly, "YourWebApiProject.SwaggerExtensions.index.html");
  206. // If your API has multiple versions and you've applied the MultipleApiVersions setting
  207. // as described above, you can also enable a select box in the swagger-ui, that displays
  208. // a discovery URL for each version. This provides a convenient way for users to browse documentation
  209. // for different API versions.
  210. //
  211. //c.EnableDiscoveryUrlSelector();
  212. // If your API supports the OAuth2 Implicit flow, and you've described it correctly, according to
  213. // the Swagger 2.0 specification, you can enable UI support as shown below.
  214. //
  215. //c.EnableOAuth2Support(
  216. // clientId: "test-client-id",
  217. // clientSecret: null,
  218. // realm: "test-realm",
  219. // appName: "Swagger UI"
  220. // //additionalQueryStringParams: new Dictionary<string, string>() { { "foo", "bar" } }
  221. //);
  222. // If your API supports ApiKey, you can override the default values.
  223. // "apiKeyIn" can either be "query" or "header"
  224. //
  225. //c.EnableApiKeySupport("apiKey", "header");
  226. });
  227. }
  228. }
  229. }