ASP.Net MVC - TempData Session Problem

The WebFarm used does not support the session. We must transmit data during the redirection. How to do this without the TempData dictionary, because TempData uses Session internally.

+4
source share
3 answers

You can write your own TempData provider and store it in cookies. See ASP.NET MVC Store TempData in Cookie . Or you may have a base class controller that searches for hidden input and moistens objects / state and saves it / from it every http request.

Note that TempData is only saved between two controller actions.

Edit:

You can use the same example and write a provider that serializes to DB ... or ... even to disk. Shoot, for that matter, you can even collapse the entire custom replacement for the session. You must create a factory session class and store your custom session objects using the key in some static collection. You will then track this session key either through cookies or through hidden input, as described above.

+3
source

This was a very helpful question to learn more at MVC. Although I had a question about why Microsoft suggests that people will know that TempData is using a session.

I have a problem loading objects larger than 4 KB. To do this, our architect proposed to divide this object and save them in pieces in cookies. I used the code from the blog below to split the serialized line of an object.

http://lukencode.com/2010/04/21/split-string-into-array-of-chunks/

So, split the cookie in the SaveTempData method and collect them on one line in LoadTempData. This problem has been resolved.

But I use distributed caching technology like NVElocity is always better.

0
source

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

Download:

 string mydata = GlobalStorage.Device["myKey"].ToString(); 
0
source

Source: https://habr.com/ru/post/1338843/


All Articles