How to assign a dictionary value to a session in ASP.NET Core?
I getting an error:
Cannot convert from Dictionary<string, bool> to string
//Role
var query2 = (from a in _db.MuRole
select new { a.RoleName, a.RolePiority }).ToList();
var lvRole = "";
Dictionary<string, bool> dicRole = new Dictionary<string, bool>();
foreach (var item in query2)
{
lvRole = item.RoleName;
//bool status = function check();
dicRole.Add(item.RoleName, false);
}
HttpContext.Session.SetString("Role", dicRole);
SessionExtensions.SetString(ISession, String, String)Method only supported providedvaluewithstringtype only.To store an object in Session, you need to serialize the object (To write JSON to a string or to a file).
If you install
Newtonsoft.Jsonlibrary,Or using the default
JsonSerializer.To get the object from the session, you have to deserialize the string.