In ASP.Net Core 2.2 application Session["obj"] will not work. The session is working on ASP.Net Core 2.2 application but in a different way. Here I will describe how you will work with a session in core 2.2 applications.
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.IsEssential = true;
options.IdleTimeout = TimeSpan.FromMinutes(20);//You can set Time
});
app.UseSession();
In controller,
For Setting data in session:
HttpContext.Session.SetInt32("sessionId", sessionid);
HttpContext.Session.SetString("sessionId", sessionid);
For getting data using session:
var s=HttpContext.Session.GetInt32("sessionId").Value;
var s=HttpContext.Session.GetString("sessionId").Value;
Namespace for SetInt32 or SetString is:
using Microsoft.AspNetCore.Http;
Like this, you will use Session in Asp.Net Core 2.2.
Step-1:
Add the following 2 lines in the ConfigureServices method.services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.IsEssential = true;
options.IdleTimeout = TimeSpan.FromMinutes(20);//You can set Time
});
Step-2:
Add the following 1 line in the Configure method.app.UseSession();
Step-3:
Now you can use the session for storing data for a temporary period.In controller,
For Setting data in session:
HttpContext.Session.SetInt32("sessionId", sessionid);
HttpContext.Session.SetString("sessionId", sessionid);
For getting data using session:
var s=HttpContext.Session.GetInt32("sessionId").Value;
var s=HttpContext.Session.GetString("sessionId").Value;
Namespace for SetInt32 or SetString is:
using Microsoft.AspNetCore.Http;
Like this, you will use Session in Asp.Net Core 2.2.
0 Comments