using Furion; using Furion.SpecificationDocument; using IGeekFan.AspNetCore.Knife4jUI; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using SqlSugar; using System.Text.Json.Serialization; using Yw.Application; namespace HStation.WebApi { /// /// /// [AppStartup(10)] public class Startup : AppStartup { /// /// /// public void ConfigureServices(IServiceCollection services) { services.AddConfigurableOptions(); services.Configure(options => { options.Limits.MaxRequestBodySize = int.MaxValue;//之前的 options.Limits.MaxRequestBufferSize = int.MaxValue; options.Limits.MaxResponseBufferSize = int.MaxValue; }); services.Configure(options => { options.MaxRequestBodySize = int.MaxValue;//之前的 options.MaxRequestBodyBufferSize = int.MaxValue; }); //解决Multipart body length limit 134217728 exceeded services.Configure(x => { x.ValueLengthLimit = int.MaxValue; x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart x.MultipartBoundaryLengthLimit = int.MaxValue; x.BufferBodyLengthLimit = int.MaxValue; }); // services.AddJwt(enableGlobalAuthorize: true); //// 拦截器 services.AddMvcFilter(); services.AddMvcFilter(); services.AddMvcFilter(); //需在 services.AddControllers() 之前注册 services.AddCorsAccessor(); services.AddControllers().AddInjectWithUnifyResult(); services.AddSwaggerGen(c => { //避免dto 同名 c.CustomSchemaIds(x => x.FullName); //支持knife4ui(必备) c.CustomOperationIds(s => { var a = s.ActionDescriptor as ControllerActionDescriptor; return a.ControllerTypeInfo.Name + "-" + s.HttpMethod + a.ActionName; }); c.UseInlineDefinitionsForEnums(); }).AddMiniProfiler(); services.AddJsonOptions(options => { //返回属性大小写问题 options.JsonSerializerOptions.PropertyNamingPolicy = null; //时间格式处理 options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter()); options.JsonSerializerOptions.Converters.Add(new DateTimeNullableJsonConverter()); //长整型格式处理 options.JsonSerializerOptions.Converters.Add(new LongJsonConverter()); options.JsonSerializerOptions.Converters.Add(new LongNullableJsonConverter()); //忽略循环引用 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // 仅.NET 6支持 }); // 添加即时通讯 services.AddSignalR(); SnowFlakeSingle.WorkId = Yw.Settings.SqlSugarParasHelper.SqlSugar.SnowFlakeWorkId; } /// /// /// public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } // 添加规范化结果状态码,需要在这里注册 app.UseUnifyResultStatusCodes(); app.UseHttpsRedirection(); #region 开放 data 文件夹(可以通过url访问文件) //app.UseStaticFiles(); string path = AppContext.BaseDirectory; path = Path.Combine(path, Yw.Settings.ServiceParasHelper.Service.DataFolder); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //通过url访问文件 app.UseStaticFiles(new StaticFileOptions()//自定义自己的文件路径 { RequestPath = new PathString(""),//对外的访问路径 之前为 "/Data" ,改动是为了隐藏Data对外显示 FileProvider = new PhysicalFileProvider(path),//指定实际物理路径 OnPrepareResponse = (stf) =>//静态资源跨域问题 { stf.Context.Response.Headers["Access-Control-Allow-Origin"] = "*"; stf.Context.Response.Headers["Access-Control-Allow-Headers"] = "*"; } }); #endregion 开放 data 文件夹(可以通过url访问文件) // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间 //app.UseSerilogRequestLogging(); app.UseRouting(); //需在 app.UseRouting(); 和 app.UseAuthentication(); 之间注册 app.UseCorsAccessor(); app.UseAuthentication(); app.UseAuthorization(); // 配置Swagger-Knife4UI(路由前缀一致代表独立,不同则代表共存) app.UseKnife4UI(options => { options.RoutePrefix = "Api"; // 配置 Knife4UI 路由地址 foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups()) { options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title); } }); app.UseInject(string.Empty); app.UseEndpoints(endpoints => { // 注册集线器 endpoints.MapHub("/hubs/chathub"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } } }