Startup.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Http.Features;
  8. using Microsoft.AspNetCore.Rewrite;
  9. using Microsoft.AspNetCore.StaticFiles;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.FileProviders;
  13. using Microsoft.Extensions.Hosting;
  14. using System.Text;
  15. using System.Linq;
  16. using Microsoft.AspNetCore.Mvc.Razor;
  17. namespace MySystem
  18. {
  19. public class Startup
  20. {
  21. public Startup(IConfiguration configuration)
  22. {
  23. Configuration = configuration;
  24. }
  25. public IConfiguration Configuration { get; }
  26. // This method gets called by the runtime. Use this method to add services to the container.
  27. public void ConfigureServices(IServiceCollection services)
  28. {
  29. services.AddControllersWithViews();
  30. services.AddRouting(options =>
  31. {
  32. options.LowercaseUrls = true;
  33. });
  34. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  35. // services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  36. services.AddMvc(options =>
  37. {
  38. options.EnableEndpointRouting = false;
  39. options.Filters.Add(typeof(GlobalExceptionsFilter));
  40. });
  41. //配置模版视图路径
  42. services.Configure<RazorViewEngineOptions>(options =>
  43. {
  44. options.ViewLocationExpanders.Add(new TemplateViewLocationExpander());
  45. });
  46. services.AddSession(options =>
  47. {
  48. // 设置 Session 过期时间
  49. options.IdleTimeout = TimeSpan.FromHours(1);
  50. options.Cookie.HttpOnly = true;
  51. });
  52. services.Configure<FormOptions>(x =>
  53. {
  54. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  55. });
  56. //生成密钥
  57. MySystemLib.SystemPublicFuction.appcheck = "success";
  58. RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
  59. }
  60. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  61. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  62. {
  63. if (env.IsDevelopment())
  64. {
  65. app.UseDeveloperExceptionPage();
  66. Library.ConfigurationManager.EnvironmentFlag = 1;
  67. }
  68. else
  69. {
  70. app.UseExceptionHandler("/Home/Error");
  71. app.UseHsts();
  72. Library.ConfigurationManager.EnvironmentFlag = 2;
  73. }
  74. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  75. app.UseStaticFiles();
  76. app.UseStaticFiles(new StaticFileOptions
  77. {
  78. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  79. {
  80. { ".apk", "application/vnd.android.package-archive" }
  81. })
  82. });
  83. app.UseCors("cors");
  84. app.UseAuthentication();
  85. app.UseRouting();
  86. app.UseAuthorization();
  87. app.UseSession();
  88. app.UseEndpoints(endpoints =>
  89. {
  90. endpoints.MapControllerRoute(
  91. name: "default",
  92. pattern: "{controller=Home}/{action=Index}/{Id?}");
  93. });
  94. StartHelper.Instance.Start(); //开启线程
  95. }
  96. }
  97. }