I need to store the application state (things that must stay in a memory until the user decides that he wants to save them to db). Now I used to use the Session of the application with Asp net forms, but in MVC I am using a static class to do that, but I percieve that it is not right. In what the scope of a static class differ from the scope of the session in an application mvc? How can I create something like a static class that uses the Session of the user instead? Some code examples? I tried to encapsulate the Session in a static class in MVC but to no success. Thanks
Asked
Active
Viewed 402 times
0
-
Possible duplicate of [ASP.Net MVC and state - how to keep state between requests](https://stackoverflow.com/questions/10756140/asp-net-mvc-and-state-how-to-keep-state-between-requests) – Daxtron2 May 25 '18 at 17:33
-
The session is always specific to the user. The reason this works is because there's a unique identifier sent to the user stored as a cookie. That session cookie is what's used to identify which session belongs to the user. So even though the `Session` object is static, it uses user information to determine which session stored in the web server to use. – Pluto May 25 '18 at 17:47
-
Use the Session in the same way in MVC -- a key-value store. `MyClass state = (MyClass)Session["Key"]`. See also [this](https://stackoverflow.com/questions/18224442/life-of-a-static-class-in-asp-net) and [this](https://stackoverflow.com/questions/194999/are-static-class-instances-unique-to-a-request-or-a-server-in-asp-net) regarding static class usage. – Jasen May 25 '18 at 17:51