using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.Cookies; 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 Microsoft.Extensions.FileProviders; using WebApiClient; using System.Globalization; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Localization; using AMESCoreStudio.Web.Code; using Microsoft.AspNetCore.Http.Features; namespace AMESCoreStudio.Web { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { // 擴充JSON services.AddControllers().AddNewtonsoftJson(); // FormPost數量上限 services.Configure(options => { options.ValueLengthLimit = 209715200; options.ValueCountLimit = int.MaxValue; }); // ModelBinding筆數上限 services.AddMvc(options => { options.MaxModelBindingCollectionSize = int.MaxValue; }); services.AddMvc().AddMvcOptions(options => { options.MaxModelValidationErrors = 999999; }); var config = Configuration.Get().VirtualPath; var fileServerOptions = new List(); if (config != null) { config.ForEach(f => { try { fileServerOptions.Add(new FileServerOptions { FileProvider = new PhysicalFileProvider(@f.RealPath), RequestPath = new PathString(f.RequestPath), }); } catch { } }); }; services.AddSingleton(new FileServerProvider(fileServerOptions)); // CORS設定 services.AddCors(options => options.AddPolicy("AMESPolicy", p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod())); services.AddLocalization(o => { o.ResourcesPath = "Resources"; }); services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(options => { options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource)); }); services.AddControllersWithViews().AddRazorRuntimeCompilation(); // Session設定 services.AddSession(); // 權限設定 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => { options.LoginPath = new PathString("/Login/Index"); options.LogoutPath = new PathString("/Login/Logout"); options.AccessDeniedPath = new PathString("/Home/Error"); options.Cookie.Name = "_AMESCookie"; options.SlidingExpiration = true; options.Cookie.HttpOnly = true; }); // HttpClient設定 var types = typeof(Startup).Assembly.GetTypes() .Where(type => type.IsInterface && ((System.Reflection.TypeInfo)type).ImplementedInterfaces != null && type.GetInterfaces().Any(a => a.FullName == typeof(IHttpApi).FullName)); foreach (var type in types) { // 設定呼叫API 等待10分鐘 services.AddHttpApi(type).ConfigureHttpClient(o => { o.Timeout = TimeSpan.FromMinutes(10); }); services.ConfigureHttpApi(type, o => { o.HttpHost = new Uri(AppSetting.Setting.ApiUrl); }); } } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IFileServerProvider fileServerprovider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseCors("AMESPolicy"); app.UseStaticFiles(); IList supportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("zh-CN"), new CultureInfo("zh-TW") }; app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }); app.UseRouting(); app.UseAuthorization(); app.UseAuthentication(); app.UseSession(); // VersionCode設定值 app.Use(async (context, next) => { var currentVersion = Configuration["VersionCode"]; var webVersion = context.Request.Cookies["VersionCode"]; if (string.IsNullOrEmpty(context.Request.Cookies["VersionCode"])) { context.Response.Cookies.Append("VersionCode", currentVersion); } else { if ( webVersion != currentVersion) { context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync("AMES版本已經更新!!
1. 請先關閉所有已經打開的網頁頁籤
2. 再重新打開AMES網頁"); } } await next.Invoke(); }); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Login}/{action=Index}/{id?}"); }); app.UseCookiePolicy(); // 檔案目錄 app.UseFileServerProvider(fileServerprovider); } } }