The TempData in Asp.net MVC is a whole

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.