Why TempData is Session Supported

I read that TempData is supported by session in Asp.Net MVC but is only valid for the duration of the current request. This makes it suitable for Redirect2Action etc. Considering that it is only valid for the current request, does that mean you won't run into issues with the load balancers and session on WebFarm. Mainly because you are only on one web server per request.

Can I instead copy it using the Request.Items collection?

+3


source to share


1 answer


TempData is only valid for the current and next request , so it persists in session state. Storing this request won't work.

Thus, any constraints that might break stored items in session state (such as load balancing) would also violate TempData.



See documentation for TempDataDictionary

You can use a TempDataDictionary object to pass data in the same way that you use a ViewDataDictionary object. However, the data in the TempDataDictionary object is only retained from one request to the next, unless you have marked one or more keys to be saved using the Keep method. If the key is checked for save, the key is saved for the next request.

A typical use of a TempDataDictionary object is to pass data from when it redirects to another action method. For example, an action method can store error information in a TempData Watch property (which returns a TempDataDictionary object) before calling the RedirectToAction method. The next action method can handle the error and display a view that displays the error message.

+4


source







All Articles