0

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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="msdn.microsoft.com/en-us/library/… a look what MS says about Session</a> :)
1

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.