The WebFarm we are using doesn't supports Session. We are in a requirement to pass Data during redirects. How to do this without TempData dictionary since TempData uses Session inside.
3 Answers
You can write your own TempData provider and store it in cookies. See ASP.NET MVC Store TempData in Cookie Or you could have a base-class Controller that looks for a hidden input and hydrates objects / state and persists it to / from it each http request.
Note that TempData only persists between two controller actions.
Edit:
You could use the same example and write a provider that serializes to a DB ... or ... even to disk. Shoot, for that matter, you could even roll an entire custom replacement for Session. You'd create a session factory class and store your custom session objects via a key in some static collection. Then you'd track that session key either through cookies or via hidden input as stated above.
-
That's a good solution.But if my object size is huge then it will be a problem. Cookies allow maximum size 4 kb.In that case can you please give a good solution ? Can we use Velocity like cache providers ? – Thanigainathan Feb 08 '11 at 16:46
-
I liked what you said about just creating our own class to deal with it. I created my own Dictionary
I stored data to, in a public class I could import with a `using` statement to bring in its namespace when I needed it. This should work in any case when TempData, ViewData, Session, and Cookies don't. – vapcguy Mar 25 '15 at 06:27
Create a class that looks like this:
public class GlobalStorage
{
public static Dictionary<string, object> Device = new Dictionary<string, object>();
}
Store:
GlobalStorage.Device.Add("myKey", mydata);
Retrieve:
string mydata = GlobalStorage.Device["myKey"].ToString();

- 7,097
- 1
- 56
- 52
This was a very very useful question to learn more in MVC. Though I got some question like why Microsoft is assuming that people will know TempData uses session.
I got problem with uploading objects more than 4kb. For that our architect suggessted to split that object and save them in chunks in Cookies. I used the code from below blog to split the serialized string of the object.
http://lukencode.com/2010/04/21/split-string-into-array-of-chunks/
So split the cookie in the SaveTempData method and collect them into single string in LoadTempData. Thats it problem solved.
But I using a distributed caching technology like NVElocity is always better .

- 1,505
- 14
- 25