Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TempData Like Object in WebForms - Session State for only 1 Additional Request

I would like to store some objects only for one request through the session state. I can't seem to think of an easy way to accomplish this. This is exactly what ASP.NET MVC's TempData object does. Could anyone provide me with a link or some examples of how to have an object in session state only survive one additional request?

I was thinking, this could be possibly accomplished by making a custom dictionary object, which stores an age (# of requests) on each item. By subscribing to the Application_BeginRequest and Application_EndRequest methods, you could perform the required cleanup of the objects. This could even probably facilitate making an object that stored a piece of data for X requests, not just one. Is this on the right track?

like image 611
Ryan Hoffman Avatar asked Aug 29 '09 00:08

Ryan Hoffman


People also ask

Is TempData session specific?

1) Both TempData and Session requires typecasting for getting data and check for null values to avoid run time exceptions. 2) If we read from session and then destroy it immediately then it is similar to TempData e.g. var data = Session["SomeValue"];

What will happen if we are using TempData in controller and we have disabled session?

TempData in MVC uses session state internally to store the data. So when we disable the session state for the controller, it will throw the exception as below.

How can I store data temporarily in MVC like session?

TempData is a dictionary object to store data temporarily. It is a TempDataDictionary class type and instance property of the Controller base class. TempData is able to keep data for the duration of a HTP request, in other words it can keep live data between two consecutive HTTP requests.

What is TempData in MVC with example?

TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class. So, it is available in any controller or view in the ASP.NET MVC application. The following example shows how to transfer data from one action method to another using TempData.


1 Answers

I implemented something very similar to what you describe in the Application_AcquireRequestState method of Global.ascx.cs. All my session objects are wrapped in a class that keeps count of the number of reads.

// clear any session vars that haven't been read in x requests
List<string> keysToRemove = new List<string>();
for (int i = 0; HttpContext.Current.Session != null && i < HttpContext.Current.Session.Count; i++)
{
    var sessionObject = HttpContext.Current.Session[i] as SessionHelper.SessionObject2;
    string countKey = "ReadsFor_" + HttpContext.Current.Session.Keys[i];
    if (sessionObject != null/* && sessionObject.IsFlashSession*/)
    {
        if (HttpContext.Current.Session[countKey] != null)
        {
            if ((int)HttpContext.Current.Session[countKey] == sessionObject.Reads)
            {
                keysToRemove.Add(HttpContext.Current.Session.Keys[i]);
                continue;
            }
        }
        HttpContext.Current.Session[countKey] = sessionObject.Reads;
    }
    else if (HttpContext.Current.Session[countKey] != null)
    {
        HttpContext.Current.Session.Remove(countKey);
    }
}

foreach (var sessionKey in keysToRemove)
{
    string countKey = "ReadsFor_" + sessionKey;
    HttpContext.Current.Session.Remove(sessionKey);
    HttpContext.Current.Session.Remove(countKey);
}
like image 158
Adam Avatar answered Nov 13 '22 13:11

Adam