54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using AcdiuTools.Services;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// 1. 添加 MVC 控制器支持
|
||
builder.Services.AddControllersWithViews();
|
||
|
||
// 2. 注册 HttpContextAccessor (之前已添加,确保存在)
|
||
builder.Services.AddHttpContextAccessor();
|
||
|
||
// 3. 注册 Session 服务
|
||
// AddSession 必须放在 AddControllersWithViews 之后或之前均可,但必须在 Build 之前
|
||
builder.Services.AddSession(options =>
|
||
{
|
||
options.IdleTimeout = TimeSpan.FromMinutes(30); // Session 过期时间
|
||
options.Cookie.HttpOnly = true;
|
||
options.Cookie.IsEssential = true; // 即使未同意 Cookie 策略也发送(用于功能必需)
|
||
});
|
||
|
||
// 注册主题服务
|
||
builder.Services.AddScoped<IThemeService, ThemeService>();
|
||
|
||
var app = builder.Build();
|
||
|
||
// 4. 配置中间件管道
|
||
// UseSession 必须放在 UseRouting 之后,UseAuthorization 和 MapControllers 之前
|
||
if (!app.Environment.IsDevelopment())
|
||
{
|
||
app.UseExceptionHandler("/Home/Error");
|
||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||
app.UseHsts();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseRouting();
|
||
|
||
// 【关键】启用 Session 中间件
|
||
app.UseSession();
|
||
// 启用静态文件中间件,确保 wwwroot 中的资源可访问
|
||
app.UseStaticFiles();
|
||
|
||
app.UseAuthorization();
|
||
|
||
|
||
app.MapStaticAssets();
|
||
|
||
app.MapControllerRoute(
|
||
name: "default",
|
||
pattern: "{controller=Home}/{action=Index}/{id?}")
|
||
.WithStaticAssets();
|
||
|
||
|
||
app.Run();
|