You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

196 lines
6.7 KiB

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)
{
// ÂX¥RJSON
services.AddControllers().AddNewtonsoftJson();
// FormPost¼Æ¶q¤W­­
services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = 209715200;
options.ValueCountLimit = int.MaxValue;
});
// ModelBindingµ§¼Æ¤W­­
services.AddMvc(options =>
{
options.MaxModelBindingCollectionSize = int.MaxValue;
});
services.AddMvc().AddMvcOptions(options =>
{
options.MaxModelValidationErrors = 999999;
});
var config = Configuration.Get<VirtualPathConfig>().VirtualPath;
var fileServerOptions = new List<FileServerOptions>();
if (config != null)
{
config.ForEach(f =>
{
try
{
fileServerOptions.Add(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(@f.RealPath),
RequestPath = new PathString(f.RequestPath),
});
}
catch { }
});
};
services.AddSingleton<IFileServerProvider>(new FileServerProvider(fileServerOptions));
// CORS³]©w
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³]©w
services.AddSession();
// Åv­­³]©w
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³]©w
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)
{
// ³]©w©I¥sAPI µ¥«Ý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<CultureInfo> supportedCultures = new List<CultureInfo>
{
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³]©w­È
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ª©¥»¤w¸g§ó·s!!<br>1. ½Ð¥ýÃö³¬©Ò¦³¤w¸g¥´¶}ªººô­¶­¶ÅÒ<br>2. ¦A­«·s¥´¶}AMESºô­¶");
}
}
await next.Invoke();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
app.UseCookiePolicy();
// Àɮץؿý
app.UseFileServerProvider(fileServerprovider);
}
}
}