ASP.NET 5 (Core): How to store objects in session-cache (ISession)?

I am writing an ASP.NET 5 MVC 6 (Core) application. Now I came to a point where I need to store (set and get) an object in the session-cache (ISession).

As you may know, the Set-method of ISession takes a byte-array and the Get-method returns one.

In a non-core-application I would use the BinaryFormatter to convert my object. But how can I do it in a core-application?


I'd go with serializing the objects to JSON and use the extensions methods on ISession to save them as string's.

// Save
var key = "my-key";
var str = JsonConvert.SerializeObject(obj);
context.Session.SetString(key, str);

// Retrieve
var str = context.Session.GetString(key);
var obj = JsonConvert.DeserializeObject<MyType>(str);

The extension methods on ISession are defined in the Microsoft.AspNet(Core).Http namespace.

Need Your Help

How to display all table row contents of my database in PHP?

php mysql image tablerow

I'm trying to do a shopping gallery. I managed to display my database table's first row content (content includes: image thumbnail of product, product name and product price). now i'm trying to dis...

User Control Docking Attribute

c# winforms user-controls attributes docking

I am trying to make my own user control and have almost finished it, just trying to add some polish. I would like the option in the designer to "Dock in parent container". Does anyone know how to d...