添加项目文件。
This commit is contained in:
50
Program.cs
Normal file
50
Program.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
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();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapStaticAssets();
|
||||
|
||||
app.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}")
|
||||
.WithStaticAssets();
|
||||
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user