Hi, how should I store user data in asp.net mvc? Let's say a user want to see 50 records per page. I wanted to save it in Session, but if I am doing it right, the Session resets every time a new controller is initialized. So where? A cookie?
Typically the session is not reset on controller initialization! Make sure you aren't clearing the session from code. Anyway, storing this in session cause the record-limit to be reset quite often (depend on the session timeout param).
Consider storing this in the user's profile kept in database (will be used after log in), or in cookie (don't require login to be used). This will keep this setting forever - your users will appreciate that :)
Session does not reset when a new controller is initialized. But it does when you leave the application (your session ends) or the application is restarted. You should use Profile to store this kind of information.
See this: http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx http://www.odetocode.com/articles/440.aspx
Instead of using the built in ProfileProvider-system in ASP.NET, which you should only use i you want to persist user settings across multiple visits, you could instead put a the settingsdata in the session. Maybe wrapped in a serializable object.
Session is cleared if
- you clear it in your code
- the cookie storing the sessionid expires (depends on your settings i web.config) (if a cookie expires during a session, it does not truly expire before the user closes all browser windows)
- if the application is restarted (unless you use sticky sessions (DB based sessions) in which case sessiondata persists through application restart)