¶Ô±ÈÐÂÎļþ |
| | |
| | |  |
| | | using Microsoft.Extensions.DependencyInjection; |
| | | using Microsoft.OpenApi.Models; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Runtime.InteropServices; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Helper; |
| | | using static WIDESEA_Core.Extensions.CustomApiVersion; |
| | | using Swashbuckle.AspNetCore.Filters; |
| | | |
| | | namespace WIDESEA_Core.Extensions |
| | | { |
| | | /// <summary> |
| | | /// Swagger |
| | | /// </summary> |
| | | public static class SwaggerSetup |
| | | { |
| | | /// <summary> |
| | | /// Swagger |
| | | /// </summary> |
| | | /// <param name="services"></param> |
| | | /// <exception cref="ArgumentNullException"></exception> |
| | | public static void AddSwaggerSetup(this IServiceCollection services) |
| | | { |
| | | if (services == null) throw new ArgumentNullException(nameof(services)); |
| | | |
| | | var basePath = AppDomain.CurrentDomain.BaseDirectory; |
| | | //var basePath2 = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath; |
| | | var ApiName = AppSettings.app(new string[] { "ApiName" }); |
| | | |
| | | services.AddSwaggerGen(c => |
| | | { |
| | | //éååºå
¨é¨ççæ¬ï¼åææ¡£ä¿¡æ¯å±ç¤º |
| | | typeof(ApiVersions).GetEnumNames().ToList().ForEach(version => |
| | | { |
| | | c.SwaggerDoc(version, new OpenApiInfo |
| | | { |
| | | Version = version, |
| | | Title = $"{ApiName} æ¥å£ææ¡£ââ{RuntimeInformation.FrameworkDescription}", |
| | | Description = $"{ApiName} HTTP API " + version, |
| | | //Contact = new OpenApiContact { Name = ApiName, Email = "Blog.Core@xxx.com", Url = new Uri("https://neters.club") }, |
| | | //License = new OpenApiLicense { Name = ApiName + " 宿¹ææ¡£", Url = new Uri("http://apk.neters.club/.doc/") } |
| | | }); |
| | | //c.OrderActionsBy(o => o.RelativePath); |
| | | }); |
| | | |
| | | c.UseInlineDefinitionsForEnums(); |
| | | try |
| | | { |
| | | var xmlPath = Path.Combine(basePath, "WIDESEA_WMSServer.xml"); |
| | | //é»è®¤ç第äºä¸ªåæ°æ¯falseï¼è¿ä¸ªæ¯controllerçæ³¨éï¼è®°å¾ä¿®æ¹ |
| | | c.IncludeXmlComments(xmlPath, true); |
| | | |
| | | var xmlModelPath = Path.Combine(basePath, "WIDESEA_Model.xml"); |
| | | c.IncludeXmlComments(xmlModelPath); |
| | | |
| | | var xmlDTOPath = Path.Combine(basePath, "WIDESEA_DTO.xml"); |
| | | c.IncludeXmlComments(xmlDTOPath); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | //log.Error("Blog.Core.xmlåBlog.Core.Model.xml 丢失ï¼è¯·æ£æ¥å¹¶æ·è´ã\n" + ex.Message); |
| | | } |
| | | |
| | | // å¼å¯å æå°é |
| | | c.OperationFilter<AddResponseHeadersFilter>(); |
| | | c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>(); |
| | | |
| | | // å¨header䏿·»å tokenï¼ä¼ éå°åå° |
| | | //c.OperationFilter<SecurityRequirementsOperationFilter>(); |
| | | |
| | | c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme |
| | | { |
| | | Description = "JWTæætokenåé¢éè¦å ä¸å段Bearerä¸ä¸ä¸ªç©ºæ ¼,å¦Bearer token", |
| | | Name = "Authorization", |
| | | In = ParameterLocation.Header, |
| | | Type = SecuritySchemeType.ApiKey, |
| | | BearerFormat = "JWT", |
| | | Scheme = "Bearer" |
| | | }); |
| | | |
| | | c.AddSecurityRequirement(new OpenApiSecurityRequirement |
| | | { |
| | | { |
| | | new OpenApiSecurityScheme |
| | | { |
| | | Reference = new OpenApiReference { |
| | | Type = ReferenceType.SecurityScheme, |
| | | Id = "Bearer" |
| | | } |
| | | }, |
| | | new string[] { } |
| | | } |
| | | }); |
| | | }).AddControllers() |
| | | .ConfigureApiBehaviorOptions(options => |
| | | { |
| | | options.SuppressConsumesConstraintForFormFileParameters = true; |
| | | options.SuppressInferBindingSourcesForParameters = true; |
| | | options.SuppressModelStateInvalidFilter = true; |
| | | options.SuppressMapClientErrors = true; |
| | | options.ClientErrorMapping[404].Link = |
| | | "https://*/404"; |
| | | }); |
| | | //services.AddSwaggerGenNewtonsoftSupport(); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// èªå®ä¹çæ¬ |
| | | /// </summary> |
| | | public class CustomApiVersion |
| | | { |
| | | /// <summary> |
| | | /// Apiæ¥å£çæ¬ èªå®ä¹ |
| | | /// </summary> |
| | | public enum ApiVersions |
| | | { |
| | | /// <summary> |
| | | /// V1 çæ¬ |
| | | /// </summary> |
| | | V1 = 1, |
| | | /// <summary> |
| | | /// V2 çæ¬ |
| | | /// </summary> |
| | | V2 = 2, |
| | | } |
| | | } |
| | | } |