In earlier version of Asp.Net session can be accessed in any page like a static variable using
System.Web.HttpContext.Current.Session[\"key\"]
If you want to store and retrieve a complex object from Session, you could instead use these extensions:
public static class SessionExtensions
{
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObjectFromJson(this ISession session, string key)
{
var data = session.GetString(key);
if (data == null)
{
return default(T);
}
return JsonConvert.DeserializeObject(data);
}
}
Then you would call them like this:
User user = new User();
user.Name = "Jignesh Trivedi";
user.Percentage = 75.45;
HttpContext.Session.SetComplexData("UserData", user);
Or,
ViewBag.data = HttpContext.Session.GetComplexData("UserData");
For details, please see https://www.c-sharpcorner.com/article/session-state-in-asp-net-core/