Search code examples
asp.net-mvcsessionviewdata

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


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


Solution

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