Changes to the TempData Version - Reasons for Change

In ASP.NET MVC 2, the write lifetime in TempDataDictionary was just one HTTP request.

This translates to setting the value to one request, redirecting and accessing the same element at the other end of the line. After that, the entry will be unavailable, regardless of whether you read the value from the dictionary at the last end of the line or not.

Since ASP.NET MVC 3 (I believe), this implementation detail has changed significantly.

Entries in TempDataDictionary now deleted only after they are 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 the items in the TempData collection in one request and immediately read them back in the next next request, the functionality seems to be the same draft.

In scenarios where this is not the case, for example, if you want to read a TempData record, if it is redirected to one place, and expecting it to be deleted when requesting other resources and navigating backwards, this change has a big impact.

There is no longer an entry available for one HTTP request, but available for many HTTP requests, be it available for only one dictionary.

I would like to know more about this change in implementation, what were the reasons for this change, was it just to serve multiple redirects, or are there deeper benefits?

In addition to this, I am intrigued to find out if there is anything inline that now serves a single HTTP request for data sharing in the same way that TempData used for maintenance?

+9
asp.net-mvc asp.net-mvc-4 asp.net-mvc-2
Feb 15 '13 at 9:36
source share
1 answer

You are right that TempData keys are cleared only if they were read (or after the end of the user session), but it was the way it was after MVC2, ( http://forums.asp.net/post/3692286. aspx )

I would like to know more about this change of implementation, what were the reasons for the change, is it just to satisfy several redirects or are there deeper benefits?

This change prevented problems that occurred in MVC 1, such as TempData keys, which were removed before they were read. So yes, the main advantage is to avoid these problems when you have multiple redirects or intermittent requests. In addition, the RedirectToRouteResult or RedirectResult methods now automatically call TempData.Keep() to prevent keys from being cleared even after they are read, so keep this in mind.

In scenarios where this is not the case, for example, wanting to read the TempData record when redirecting to one place and expecting it to be deleted if you request other resources and move backward, this change has a big impact.

Are you correct if you encoded under the assumption that TempData keys are cleared automatically, you may encounter unexpected problems. You can call TempData.Clear() to manually remove all keys from TempDataDictionary or TempData.Remove(key) to remove a specific key. You can also use TempData.Peek() to read the value of the TempData key without putting it to remove from TempDataDictionary .

In addition to this, I am intrigued, know if there is anything that now serves a single HTTP request for sharing data in the same one that TempData used for maintenance?

I do not know any new objects or functions that replicate the original implementation of TempData. Essentially, we still use TempData , but we must remember that the data is saved until you read and clear the dictionary manually, if necessary.

+6
Feb 19 '13 at 20:48
source share



All Articles