Var a = TempData ["a"];
Var alsoA = TempData ["a"];
// True
Debug. Assert (Object. ReferenceEqual (a, alsoA ));
========================
Objects within TempData will only be removed till the end of current request, if during which period you havn't set something new/old with the key. sounds simple, but to me, it's not.
And here is a much simpler model, every piece of data got removed when ever you try go get it:
/// <Summary>
/// TempData is a whole, all it's value exits in the whole request life time.
/// So here is Flash, any item will be removed whenever you try to get it, much simpler model.
/// </Summary>
Public class Flash
{
Private readonly HttpSessionState session;
Public static Flash Instance
{
Get
{
Return new Flash (HttpContext. Current );
}
}
Private Flash (HttpContext context)
{
Session = context. Session;
}
Public object this [string key]
{
Get
{
Var sessionKey = GetSessionKey (key );
Var obj = session [sessionKey];
Session. Remove (sessionKey );
Return obj;
}
Set
{
Var sessionKey = GetSessionKey (key );
Session [sessionKey] = value;
}
}
Private static string GetSessionKey (string key)
{
Return "_ Flash_Data _ {0}". Form (key );
}
}