visual studio 2022 でSessionサービスを追加。
自分の読んでる本だとStartupクラスがある前提なので
それがない現在はどうするのか調べてみた。
Program.cs の下記に記述すればいいみたい。
*参考にしたページ:https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/app-state?view=aspnetcore-6.0
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDistributedMemoryCache(); //ここから
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(100);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
}); //ここまで
var app = builder.Build();
// Configure the HTTP request pipeline.
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.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession(); //あとここ
app.MapControllerRoute(
name: "default",
pattern: "{controller=Hello}/{action=Index}/{id?}");
app.Run();