Startup.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Linq;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Http.Features;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.SpaServices.Webpack;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. namespace TEAMModelOS
  12. {
  13. public class Startup
  14. {
  15. public Startup(IConfiguration configuration, IHostingEnvironment env )
  16. {
  17. Configuration = configuration;
  18. var builder = new ConfigurationBuilder()
  19. .SetBasePath(env.ContentRootPath)
  20. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  21. .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);//增加环境配置文件,新建项目默认有
  22. this.Configuration = builder.Build();
  23. }
  24. public IConfiguration Configuration { get; }
  25. // This method gets called by the runtime. Use this method to add services to the container.
  26. public void ConfigureServices(IServiceCollection services )
  27. {
  28. //services.Configure<CookiePolicyOptions>(options =>
  29. //{
  30. // options.CheckConsentNeeded = context => true;
  31. // options.MinimumSameSitePolicy = SameSiteMode.None;
  32. //});
  33. services.AddMvc(
  34. //option=> { option.Filters.Add<HttpGlobalExceptionFilter>(); }
  35. ).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  36. //上传文件最大处理
  37. services.Configure<FormOptions>(x =>
  38. {
  39. x.ValueLengthLimit = int.MaxValue;
  40. x.MultipartBodyLengthLimit = long.MaxValue; // In case of multipart
  41. x.MultipartHeadersLengthLimit = int.MaxValue;
  42. });
  43. }
  44. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  45. #if DEBUG
  46. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  47. #else
  48. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  49. #endif
  50. {
  51. if (env.IsDevelopment())
  52. {
  53. app.UseDeveloperExceptionPage();
  54. // Webpack initialization with hot-reload.
  55. app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
  56. {
  57. HotModuleReplacement = true,
  58. });
  59. }
  60. else
  61. {
  62. app.UseExceptionHandler("/Home/Error");
  63. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  64. app.UseHsts();
  65. }
  66. // 如果你想使用官方认证,必须在上边ConfigureService 中,配置JWT的认证服务 (.AddAuthentication 和 .AddJwtBearer 二者缺一不可)
  67. app.UseAuthentication();
  68. app.UseHttpsRedirection();
  69. app.UseStaticFiles();
  70. //app.UseCookiePolicy();
  71. app.UseMvc(routes =>
  72. {
  73. routes.MapRoute(
  74. name: "default",
  75. template: "{controller=Home}/{action=Index}/{id?}");
  76. routes.MapSpaFallbackRoute(
  77. name: "spa-fallback",
  78. defaults: new { controller = "Home", action = "Index" });
  79. });
  80. }
  81. }
  82. }