views:

216

answers:

3

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?

+2  A: 

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 :)

twk
Ok, I thought it would be my fault and I though of the db profile, but could you please explain when does Session resets? Or how to use it correctly?
Trimack
The session time-out refers to the number of minutes before the user session is aborted after last request. It is set in web.config (or IIS site config).By default, the session is stored InProc which means every restart of your application will clear the session (for example: when you publish new dll version(s) to the /bin directory).You can store the session state in a separate process (SessionState service in windows) or event in SQL db.Please <a href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx">take a look what MS says about Session</a> :)
twk
Link here:http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx
twk
A: 

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

gius
+1  A: 

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)
Lars Udengaard