0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Sessionサービスの追加 visual studio 2022

Posted at

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();


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?