ASP.NET MVC: TempData vs Session and When to use what?

TempData vs Session and When to use what in asp.net mvcIntroduction: In this article you will learn the following:

1) What is the Difference and Similarity between TempData and Session?
2) When to use TempData and Session?



Differences between TempData and Session In Asp.Net MVC

TempData
Session
TempData is dictionary object derived from TempDataDictionary class.
Session is an object derived from HttpSessionState class. 
TempData is property of ControllerBase class. 
Session is a property of HttpContext class.

TempData is used to pass data from one action to another action of same or different controller. In other words when we redirect, it helps to maintain data between those redirects.
Session is also used to pass data within application and but it persists until session expires (by default session expiration time is 20 minutes but can be increased).
TempData’s life is very short and lasts until the next request is received.
Session is valid for all requests, not for a single redirect.
It valid for only current and subsequent request only
It valid for all requests.
TempData has following two overloaded versions of Keep method to persist the value of TempData for next request. 

1) TempData.Keep() to persist all TempData objects
2) TempData.Keep(“BookName”) to persist specific TempData object.
No such methods are there for session because value is available for all requests until session expires.
TempData internally stores value in to Session State.
Session variable are stored in SessionStateItemCollection object (Which is exposed through the HttpContext.Session property of page).
TempData stores its content in Session state but its value gets automatically deleted from session on the next request i.e. earlier than a session object.

As TempData gets destroyed immediately after it’s read in subsequent HTTP request or until the session times out, we don't need to care about destroying it explicitly.
The Session on the other hand is stored at the application level (for each user) and values added to it will only be removed if the application pool restarts (or IIS restarts / times out) or after the Session time out occurred. The value can be explicitly removed from the Session (via the Remove() method .




Similarities between TempData and Session In Asp.Net MVC

1) Both TempData and Session requires typecasting for getting data and check for null values to avoid run time exceptions.

2) If we read from session and then destroy it immediately then it is similar to TempData e.g.
var data = Session["SomeValue"];
Session.Remove("data");


When to use what?
TempData
Session
If you want pass data from one action to another action of same or different controller then use TempData.
Use Session when the data need to be maintained and available for multiple requests throughout the application.
TempData is ideal for short-lived things like displaying validation errors or other messages that we don't need to persist for longer than a single request.
Session is ideal choice when we need to persist data for extended periods of time i.e. it is used to store data which is required throughout user session.

 Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.  
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..