3

I need some help. I have these two methods to save and retrieve a class in a session:

public static class SessionExtensions
    {
        public static void Set<T>(this ISession session, string key, T value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        public static T Get<T>(this ISession session, string key)
        {
            var value = session.GetString(key);
            return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
        }
    }

I need a method to save and retrieve in a session an object of type List , how should I implement for ASP.Net Core?

Thanks.

Evandro Anholeto
  • 67
  • 1
  • 2
  • 4

1 Answers1

1

You should be able to use methods like below:

////  your list object
List<MyClass> classCollection = new List<MyClass>();

//// To set value in session
HttpContext.Session.Set<List<MyClass>>("someKey", classCollection);

//// To Get Value from Session
List<MyClass> classCollection = HttpContext.Session.Get<List<MyClass>>("someKey");

Hope this helps.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37