Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lifespan of each data storage area in ASP .net MVC

Tags:

asp.net-mvc

I've seen some explanations of these, but nothing that really compares where they start, end, or overlap, or good examples of their use.

What is the life span of each of the following data collections? And am I missing any?

  • Application
  • Session
  • ViewData
  • TempData
like image 861
Josh Avatar asked Jan 03 '11 21:01

Josh


2 Answers

application: as long as your application is running. your application may be automatically shutdown and restarted by the server for various reasons

session: as long as the user is actively using your site. this is generally determined by cookies that ASP.NET sends down to give each user a unique ID that expires after a while. there are lots of ways to customize & tweak this to meet various needs

viewdata: as long as the current request is being processed. this is used for sending data from a controller to a view for immediate rendering and thus not persisted

tempdata: until the value is read back out OR until the end of processing the next request in the session OR when the session ends/expires - whichever is sooner. this is meant to be used for moving data from one controller to another when you are issuing a Redirect

like image 96
Robert Levy Avatar answered Sep 19 '22 12:09

Robert Levy


Application : This get initiated at the time when an application start and end when the application stops the execution.If user leaves the application domain or application gets restarted then also the application based data is lost.

Session : This is application based storage. This ends when user leaves the current request or the session get expired. It can be stored in several modes like application cookie or client side cookie.

ViewBag & ViewData : This storage method hold the data for the current request. It transport the data between view and controller.

TempData : Lifespan of this storage type depends on, at which request the Tempdata is read. Once it is read by program it gets destroyed. But we can increase its lifespan using peek or keep methods.

like image 30
Rajan Mishra Avatar answered Sep 20 '22 12:09

Rajan Mishra