Startup.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Http.Features;
  9. using Microsoft.AspNetCore.HttpsPolicy;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.SpaServices.Webpack;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using TEAMModelOS.SDK.Context.Configuration;
  15. using TEAMModelOS.SDK.Context.Filter;
  16. using TEAMModelOS.SDK.Extension.HttpClient;
  17. using TEAMModelOS.SDK.Extension.JwtAuth;
  18. using TEAMModelOS.SDK.Extension.JwtAuth.Filters;
  19. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  20. using TEAMModelOS.Service.Core.Interfaces;
  21. namespace TEAMModelOS.Admin
  22. {
  23. public class Startup
  24. {
  25. public Startup(IConfiguration configuration, IHostingEnvironment env)
  26. {
  27. Configuration = configuration;
  28. var builder = new ConfigurationBuilder()
  29. .SetBasePath(env.ContentRootPath)
  30. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  31. .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);//增加环境配置文件,新建项目默认有
  32. this.Configuration = builder.Build();
  33. BaseConfigModel.SetBaseConfig(Configuration, env.ContentRootPath, env.WebRootPath);
  34. }
  35. public IConfiguration Configuration { get; }
  36. // This method gets called by the runtime. Use this method to add services to the container.
  37. public void ConfigureServices(IServiceCollection services)
  38. {
  39. //services.Configure<CookiePolicyOptions>(options =>
  40. //{
  41. // // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  42. // options.CheckConsentNeeded = context => true;
  43. // options.MinimumSameSitePolicy = SameSiteMode.None;
  44. //});
  45. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  46. //上传文件最大处理
  47. services.Configure<FormOptions>(x =>
  48. {
  49. x.ValueLengthLimit = int.MaxValue;
  50. x.MultipartBodyLengthLimit = long.MaxValue; // In case of multipart
  51. x.MultipartHeadersLengthLimit = int.MaxValue;
  52. });
  53. //使用Blob配置
  54. services.AddAzureBlobStorage().AddConnection(Configuration.GetSection("Azure:Blob").Get<AzureBlobOptions>());
  55. //全局扫描基于IBusinessService接口的实现类
  56. services.Scan(scan => scan.FromApplicationDependencies()
  57. .AddClasses(classes => classes.AssignableTo<IBusinessService>())
  58. .AsImplementedInterfaces()
  59. .WithTransientLifetime());
  60. //引入Jwt配置
  61. services.JwtAuth(Configuration.GetSection("JwtSetting"));
  62. //HttpContextAccessor,并用来访问HttpContext。
  63. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  64. //Http https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
  65. //services.AddHttpClient();
  66. services.AddHttp();
  67. //services.AddTransient<ITeamModelUserService, ITeamModelUserService>();
  68. }
  69. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  70. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  71. {
  72. if (env.IsDevelopment())
  73. {
  74. app.UseDeveloperExceptionPage();
  75. app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
  76. {
  77. HotModuleReplacement = true
  78. });
  79. }
  80. else
  81. {
  82. app.UseExceptionHandler("/Home/Error");
  83. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  84. app.UseHsts();
  85. }
  86. app.UseHttpsRedirection();
  87. app.UseStaticFiles();
  88. //app.UseCookiePolicy();
  89. app.UseMiddleware<JwtAuthorizationFilter>();
  90. app.UseMiddleware<HttpGlobalExceptionInvoke>();
  91. app.UseMvc(routes =>
  92. {
  93. routes.MapRoute(
  94. name: "default",
  95. template: "{controller=Home}/{action=Index}/{id?}");
  96. routes.MapSpaFallbackRoute(
  97. name: "spa-fallback",
  98. defaults: new { controller = "Home", action = "Index" });
  99. });
  100. }
  101. }
  102. }