using System.Text.Encodings.Web; using System.Text.Unicode; using Gs.HostIIS; using Gs.Toolbox; using Gs.Toolbox.ApiCore.Extensions; using Gs.Toolbox.ApiCore.Group; using Microsoft.Extensions.FileProviders; using Microsoft.OpenApi.Models; using Newtonsoft.Json; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); }).AddMvcOptions(opt => { opt.Filters.Add(); }) .AddNewtonsoftJson(opt => { opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; opt.SerializerSettings.ContractResolver = new CustomContractResolver(); // opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); opt.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); builder.Services.AddCustomConvention(); builder.AddCustomInject(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddHttpContextAccessor(); builder.Services.Configure(options => { options.MaxRequestBodySize = int.MaxValue; }); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "GS-MES Api开发文档", Version = "v1" }); var _xmlPath = AppContext.BaseDirectory + AppSettingsHelper.getValueByKey("ServicesPath"); var xmlFiles = Directory.GetFiles(_xmlPath, "*.xml"); foreach (var file in xmlFiles) c.IncludeXmlComments(file, true); //添加分组定义 typeof(ApiGroupNames).GetFields().Skip(1).ToList().ForEach(f => { var info = f.GetCustomAttributes(typeof(GroupInfoAttribute), false) .OfType().FirstOrDefault(); c.SwaggerDoc(f.Name, new OpenApiInfo { Title = info?.Title, Version = info?.Version, Description = info?.Description }); }); c.SwaggerDoc("NoGroup", new OpenApiInfo { Title = "无分组" }); c.DocInclusionPredicate((docName, apiDescription) => { if (docName == "NoGroup") return string.IsNullOrEmpty(apiDescription.GroupName); return apiDescription.GroupName == docName; }); //添加安全定义 c.AddSecurityDefinition("Token", new OpenApiSecurityScheme { Description = "请输入token,格式为:token 3fa85f64-5717-4562-b3fc-2c963f66afa6(注意中间必须有空格)", Name = "token", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, BearerFormat = "JWT", Scheme = "Bearer" }); //添加安全要求 c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Token" } }, new string[] { } } }); }); builder.Services.AddSingleton(builder.Services); var app = builder.Build(); app.UseCors("AllowAll"); //添加下载路径 var _xlslPath = AppContext.BaseDirectory + AppSettingsHelper.getValueByKey("DownPath"); if (!Directory.Exists(_xlslPath)) Directory.CreateDirectory(_xlslPath); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(_xlslPath), RequestPath = "/down" }); //添加上传路径 var _upPath = AppContext.BaseDirectory + AppSettingsHelper.getValueByKey("UploadPath"); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(_upPath), RequestPath = "/upload" }); app.AddCustomController(); //if (app.Environment.IsDevelopment()) { app.UseSwagger(); //添加分组定义 app.UseSwaggerUI(options => { typeof(ApiGroupNames).GetFields().Skip(1).ToList().ForEach(f => { var info = f.GetCustomAttributes(typeof(GroupInfoAttribute), false) .OfType().FirstOrDefault(); options.SwaggerEndpoint($"/swagger/{f.Name}/swagger.json", info != null ? info.Title : f.Name); }); options.SwaggerEndpoint("/swagger/NoGroup/swagger.json", "无分组"); }); } app.UseAuthorization(); app.MapControllers(); app.Run();