0

Is it possible in ASP.NET Core to bind an instance of a C# class to a session?

So that the class lifetime is also tied to the session lifetime ~20 minutes.

I want to store somewhere the values entered by the user in a web form so that the values are saved when the user hits browser refresh or leaves the page for a short time.

Like this:

HttpContext.Session.RegisterClass(MySessionDataClass);

...

HttpContext.Session.GetObjectValue(MySessionDataClass);

// Create new MySessionDataClass or return an 
// instance of a class that has already been created for this session.
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DmitryB
  • 455
  • 1
  • 5
  • 18
  • The session api only stores byte arrays, to allow for sharing a session across web servers or restarts. You can store utf8 json.... https://stackoverflow.com/questions/35993415/asp-net-5-core-how-to-store-objects-in-session-cache-isession – Jeremy Lakeman Nov 18 '22 at 00:09
  • Can I use SessionID to create Dictionary as < SessionID, MySessionDataClass > – DmitryB Nov 18 '22 at 00:16
  • You can't bind an instance of C# class, You can serialize it and then bind.Please refer to this [link](https://stackoverflow.com/questions/73161117/how-to-getting-setting-object-value-session-in-net-core-2/73161323#73161323) – Xinran Shen Nov 18 '22 at 07:19

0 Answers0