In this article I will explain with an example, how to enable Session in ASP.Net Core (.Net Core 8).
Note: For beginners in ASP.Net Core (.Net Core 8), please refer my article ASP.Net Core 8: Hello World Tutorial with Sample Program example.
 
 

Enabling Session

Inside the Program.cs class, Session is enabled using the UseSession method of the app object.
Note: It is mandatory to call the UseSession method before the Run method.
 
Then, you need to call the AddSession method of the Services property of builder object.
Note: The default Session Timeout in ASP.Net Core is 20 minutes. If you want to set custom Session timeout, please refer my article .Net Core 8: Set custom Session Timeout in ASP.Net Core.
 
var builder = WebApplication.CreateBuilder(args);
 
//Enabling MVC
builder.Services.AddControllersWithViews();
 
builder.Services.AddSession();
 
var app = builder.Build();
app.UseSession();
//Configuring Routes
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
 
.Net Core 8: Enable Session in ASP.Net Core
 
Note: For more details on using Session in ASP.Net Core (.Net Core 8), please refer my article .Net Core 8: Using Session in ASP.Net Core.
 
 

Downloads



Other available versions