Using Session
In this recipe, we will learn how to use Session in ASP.NET Core.
Getting ready
We will be using VS 2017, and creating a controller to manipulate Session.
How to do it......
- First, we add the following library to the project:
"Microsoft.AspNetCore.Session": "2.0.0"
- Next, let's configure Session in
Startup.cs. We also add the MVC services to use the ASP.NET MVC pipeline:
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.CookieName = ".Session";
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");...