0

I was going through some links and found something new to me,

DotNetInterviewQuestion at question number 3.

Does "TempData" preserve data in the next request also?

"TempData" is available through out for the current request and in the subsequent request it's available depending on whether "TempData" is read or not.

So if "TempData" is once read it will not be available in the subsequent request.

my question is if TempData uses session internally ,then why this was done to TempData,why isn't it left like session?

when Each visitor has its own session collection which is created on first request and destroyed when session expires,then why it differs in TempData!!

i dont want to know answer about session vs tempdata ?all i want why Tempdata has variation if it use session internally,becuase either built in .net must clears the session , so why .net added this features of tempdata,albit it can be done with existing session.

Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
  • Obviously because this data is only needed after a redirect and can then be disposed, saving memory. – L-Four May 05 '15 at 13:18
  • My understanding is that it was designed for temporary data (as the name suggests) such as adding a 'success' message when you rediret from a post method to a details view. It means you do not have to manually remove the session variable each time. –  May 05 '15 at 13:19
  • my question raises different issue?,nor i want to know answer about session vs tempdata?all i want why Tempdata has variation if it use session internally @D Stanley – Shekhar Pankaj May 05 '15 at 13:22
  • @StephenMuecke yes i agree, but i found something new there..it persists many redirects until it is read.! – Shekhar Pankaj May 05 '15 at 13:23
  • It IS a session which you use for a specific case. What do you not understand about that? – L-Four May 05 '15 at 13:25
  • @ShekharPankaj, Yes that correct, because its the act of reading it that cause it to also be removed from Session (unless you use `Keep` or `Peek`) –  May 05 '15 at 13:26
  • @StephenMuecke yes then why to remove it?this is my question ?what benefits does it makes? – Shekhar Pankaj May 05 '15 at 13:30
  • Did you read my answer or the link above, where it's fully explained?? – L-Four May 05 '15 at 13:31
  • @ShekharPankaj, So you don't have to keep manually removing it from session when its finished with - it just simplifies the code. –  May 05 '15 at 13:33
  • @L-Three i can't find answer to this question by you ,and i am reading to links above,but i didn't got what i am looking for?, i am thinking how it benefits over session when it have redirection to single redirects or once read,or usinf `keep` . there should be option to set number of redirects or something. suppose i want 3 redirects to have same messsage, i need to set it again,why?then whats its advantage? – Shekhar Pankaj May 05 '15 at 13:38
  • 1
    As already said a lot of times, you use it to persist data across 1 redirect. – L-Four May 05 '15 at 13:41
  • 2
    @ShekharPankaj, you seem to be ignoring the key concept that its **Temp** (orary)Data. Its designed to be read once and disposed - that what is designed for. If you don't want that, then don't use it. –  May 05 '15 at 13:44
  • hmm,i am sensing things up,,but guyz this question isnt duplicate,why marked as duplicate? – Shekhar Pankaj May 05 '15 at 13:48
  • 1
    Because it's a duplicate and it's explained in the other answer. – L-Four May 05 '15 at 13:49

0 Answers0