Thanigainathan
Thanigainathan

Reputation: 1547

ASP.Net MVC - TempData session problem

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.

Upvotes: 4

Views: 2701

Answers (3)

vapcguy
vapcguy

Reputation: 7537

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();

Upvotes: 0

Thanigainathan
Thanigainathan

Reputation: 1547

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 .

Upvotes: 0

xanadont
xanadont

Reputation: 7604

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.

Upvotes: 3

Related Questions