In this article I will explain with an example, how to enable Session in ASP.Net Core (.Net Core 3).
Enabling Session
Inside the Startup.cs class, Session is enabled using the UseSession method of the app object inside the Configure method.
Note: It is mandatory to call the UseSession method before the UseEndpoints method.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//Enable Session.
app.UseSession();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Then, inside the ConfigureServices method, you need to call the AddSession method of the services object.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSession();
}
Downloads
Other available versions