6

When should I use one versus the other? I want to cache a certain object on startup and reuse around the application. Which sounds like the better solution (ViewData or Session)?

Jesslyn
  • 696
  • 10
  • 25
leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

12

ViewData is a per-request object used to send information from the controller to the view.
Each action invocation gets its own ViewData; the ViewData doesn't last beyond the view.

Session State is a per-user storage container which allows you to store data for a specific user session (identified by a cookie)

If you want to share a global object, you should probably make it a singleton (in a static property) or put it in Application state.
Make sure that it's thread-safe. (Or use a [ThreadStatic] field carefully)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964