using LongHuParkApi.Common; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Newtonsoft.Json; using PictureFilesApi.Services; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace PictureFilesApi { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //TODO:添加swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "图片文件API", Version = "v1" }); // 获取xml文件名 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; // 获取xml文件路径 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); // 添加控制器层注释,true表示显示控制器注释 c.IncludeXmlComments(xmlPath, true); c.OperationFilter(); }); //TODO:添加httpContext services.AddSingleton(); services.AddSingleton(); services.AddControllers(option => { }).AddNewtonsoftJson(options => { //忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //不使用驼峰样式的key //options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); //TODO:cache缓存 services.AddMemoryCache(); //TODO:跨域 services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); }); //TODO:添加过滤器 services.AddMvc(option => { option.Filters.Add(typeof(CustomActionFilter)); }); services.Configure(x => { x.ValueLengthLimit = int.MaxValue; x.MultipartBodyLengthLimit = 1073741824; x.MemoryBufferThreshold = int.MaxValue; }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用跨域配置 app.UseCors("CorsPolicy"); //可以访问根目录下面的静态文件 //静态文件选项 var staticfile = new StaticFileOptions { FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory), ContentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(new Dictionary { { ".amr","audio/AMR" }, { ".apk","application/vnd.android.package-archive" }, { ".cpg","application/vnd.android.package-archive" }, { ".dbf","application/vnd.android.package-archive" }, { ".shp","application/vnd.android.package-archive" }, { ".shx","application/vnd.android.package-archive" }, { ".zip","application/zip" }, { ".jpeg","image/jpeg" }, { ".jpg","image/jpg" }, { ".png","image/png" }, { ".mp4","video/mpeg4" }, { ".doc","application/octet-stream" }, { ".docx","application/octet-stream" }, { ".xls","application/octet-stream" }, { ".xlsx","application/octet-stream" }, { ".pdf","application/octet-stream" }, { ".pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation" }, { ".dwg","application/acad"}, { ".rar","application/x-rar-compressed"}, }) }; app.UseStaticFiles(staticfile); //TODO:添加swagger // 添加Swagger有关中间件 app.UseSwagger(); var useSwagger = Configuration.GetSection("UseSwagger").Value; if (useSwagger == "1") { app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "版本 v1"); }); } app.UseRouting(); app.UseAuthorization(); //添加Router路由设置 app.UseEndpoints(endpoints => { endpoints.Map("/", context => { context.Response.Redirect("swagger"); return Task.CompletedTask; }); endpoints.Map("weatherforecast", context => { context.Response.Redirect("swagger"); return Task.CompletedTask; }); endpoints.MapControllerRoute( name: "default", pattern: "api/{controller=Values}/{action=Index1}/{id?}" ); }); } } }