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

1

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

Irrepressible answered 7/11, 2010 at 2:20 Comment(0)
D
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)

Diecious answered 7/11, 2010 at 2:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.