In ASP.NET MVC 2, the lifespan of an entry in the TempDataDictionary
was just one HTTP Request.
That translated to setting a value in one request, redirecting, and having access to the same item at the other end of the line. After this the entry would be no longer available, regardless of whether you read the value out of the dictionary at the latter end of the line or not.
Since ASP.NET MVC 3 (I believe), this implementation detail has changed quite significantly.
Entries in the TempDataDictionary
are now only removed once they've been read.
MVC 4
public object this[string key]
{
get
{
object obj;
if (!this.TryGetValue(key, out obj))
return (object) null;
this._initialKeys.Remove(key);
return obj;
}
}
and
public bool TryGetValue(string key, out object value)
{
this._initialKeys.Remove(key);
return this._data.TryGetValue(key, out value);
}
MVC 2:
public object this[string key] {
get {
object value;
if (TryGetValue(key, out value)) {
return value;
}
return null;
}
and
public bool TryGetValue(string key, out object value) {
return _data.TryGetValue(key, out value);
}
Since most people seem to put items in the TempData
collection in one request and immediately read them back out in the immediate next request, the functionality seems roughtly the same.
In scenarios where this is not the case such as wanting to read the TempData
entry if redirected to one place, and expecting it to have been removed if requesting other resources and navigating back, this change has quite an impact.
No longer is the entry available for one http request but is available over many HTTP requests, be it only available to one single get on the dictionary.
I'd like to know more about this implimentation change, what were the reasons for the change, was this simply to cater for multiple redirects or are there deeper benefits?
Secondary to that, I'm intrigued to know if there's anything built in that now caters for single HTTP request sharing of data in the same way that TempData
used to cater for?
You're correct that
TempData
keys are only cleared if they’ve been read (or after the user’s session expires) but this has been the case since MVC2, (http://forums.asp.net/post/3692286.aspx)This change prevented problems that arose in MVC 1, such as
TempData
keys being deleted before they were read. So yes, the primary benefit is in avoiding these problems when you have multiple re-directs, or interleaved requests. In addition, theRedirectToRouteResult
orRedirectResult
methods now automatically callTempData.Keep()
to prevent clearing of keys, even after they've been read so keep that in mind as well.You’re correct, if you've been coding under the assumption that the
TempData
keys are cleared automatically you could run into unexpected problems. You can callTempData.Clear()
to manually remove all keys from theTempDataDictionary
, orTempData.Remove(key)
to remove a specific key. You can also useTempData.Peek()
to read the value of aTempData
key without flagging it for removal from theTempDataDictionary
.I'm not aware of any new objects or functions that replicate the original implementation of TempData. Essentially we still use
TempData
but have to be mindful that the data persists until read and clear the dictionary manually if needed.