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.

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.