Try
Page 1
public IActionResult PageOne()
{
HttpContext.Session.SetString("UserName", "JohnDoe");
HttpContext.Session.SetInt32("UserId", 123); // Storing an integer
// You can also store complex objects by serializing them to JSON
var userProfile = new { Name = "JaneDoe", Email = "jane@example.com" };
HttpContext.Session.SetString("UserProfile", System.Text.Json.JsonSerializer.Serialize(userProfile));
return RedirectToAction("PageTwo"); // Redirect to the target page
}
Page 2
// Example in a Controller or Razor Page code-behind
public IActionResult PageTwo()
{
string userName = HttpContext.Session.GetString("UserName");
int? userId = HttpContext.Session.GetInt32("UserId");
// Retrieve and deserialize the complex object
string userProfileJson = HttpContext.Session.GetString("UserProfile");
dynamic userProfile = null;
if (userProfileJson != null)
{
userProfile = System.Text.Json.JsonSerializer.Deserialize<dynamic>(userProfileJson);
}
return View();
}