using DS.WMS.PrintApi.Service; using DS.WMS.PrintApi.Utils; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using System.Text.Encodings.Web; using System.Text.Unicode; namespace DS.WMS.PrintApi { 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) { services.AddControllers(); //注入IWMSCommonService 公共服务 services.AddSingleton(); services.AddControllers( (options => { // 204预检查过滤 //options.Filters.Add(); //全局异常过滤 options.Filters.Add(); options.SuppressAsyncSuffixInActionNames = false; })) .AddNewtonsoftJson(options => { //CustomContractResolver 是我们自定义的解析器 options.SerializerSettings.ContractResolver = new CustomContractResolver(); // 时间格式化 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略空值 // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }) .AddJsonOptions(cfg => { //解决后端返回数据中文被编码 cfg.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); }); //Swagger services.AddSwaggerSetup(); services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // app.UseHttpsRedirection(); app.UseSwagger().UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", AppSetting.Configuration["SwaggerDoc:Version"]); }); app.UseRouting(); //静态文件 app.UseStaticFiles(); // 先开启认证 app.UseAuthentication(); // 然后是授权中间件 app.UseAuthorization(); //app.UseMiddleware(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }