What is the difference between Session and ViewData in Asp.net-MVC?

When should I use one against the other? I want to cache a specific object when starting and reusing the application. What seems like the best solution (ViewData or Session)?

+6
asp.net-mvc session
source share
1 answer

ViewData - the object for the request, used to send information from the controller to the view.
Each action call gets its own ViewData; ViewData is not beyond the scope of the presentation.

Session state is a storage container for each user that allows you to store data for a specific user session (identified cookie)

If you want to share a global object, you should probably make it a singleton (in the static property) or put it in the application state.
Make sure it is thread safe. (Or use the [ThreadStatic] field)

+12
source share

All Articles