ASP. net mvc tempdata usage experience

Source: Internet
Author: User

When reading the description of tempdata, some people say that only one request can be used. Some people say that a request will end up, one time, my code had a bug, and I had been stuck in tempdata. Finally, I found the source code. I knew that tempdata was too busy.

 

Principle

In ASP. net MVC mainly includes viewdata and tempdata. viewdata is mainly used by the Controller to distribute data to the view. There is only one action during the retention period, and tempdata must be used across actions, tempdata depends on tempdataprovider and has different retention periods. The default tempdataprovider is sessionstatetempdataprovider. You have not seen the rule. The default setting is to use the session to store tempdata, isn't the session set by the user to store the data and keep the data at 20 minutes?

Therefore, sessionstatetempdataprovider has some methods. From the very beginning of the controller, tempdata is merged into the session, and then the session is deleted. Therefore, during the action, the session of tempdata is not displayed, when retrieving tempdata, the keys used by the attacker will be recorded. At the end of the controller, any temporary tempdata will be stored in the session.No tempdata retrieved from the session will exist until the session disappears..

Note:

Retention period of viewdata

Homecontroller. CS segment public actionresult index () {This. viewdata ["data"] = "Index"; return view ();} public actionresult list () {// No data has returned return view ();}
Index. aspx fragment <div> partial: <% // viewdata uses index, and does not parse the action HTML of the List row. renderpartial ("list"); %> </div> <div> action: <% // viewdata uses the list, which indicates the action HTML of the list. renderaction ("list"); %> </div> list. ascx fragment <%: This. viewdata ["data"] %>

 

Result

 

The partial row partial or randerpartial calls the view directly in the same action and shares the same viewdata.

The specified action or randeraction calls another action, which then calls the view and uses the viewdata.

If you want to upload data in different actions, use tempdata.

Repeated failover

The following code indicates a bug.

Public actionresult index () {This. tempdata ["usedefault"] = "true"; return view ();} public actionresult list () {// In the view of the index, randeraction is used to call the list, however, that region will use ajax to repeat if (this. tempdata. containskey ("usedefault") {// call from the view of the index using randeraction and use reset to set the value ..........} else {// call from Ajax ...........} return view ();}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The answer is:

Call this. tempdata. containskey ("usedefult") is always true. Because containskey is not used, tempdata ["usedefult"] will remain in the session until the session disappears, therefore, Ajax calls always use the preset value.

 

Original shard Analysis

Login and memory

 
System. web. MVC. controller. CS fragment protected override void executecore () {// inject tempdata possiblyloadtempdata (); try {// call Action ...........} finally {// saved tempdata possiblysavetempdata ();}

 

Tempdata operations

System. web. MVC. tempdatadictionary. CS fragment // _ data is put keys + values // _ initialkeys is put keys, when used, remove key // _ retainedkeys is put with call, keep's keyspublic void load (controllercontext, itempdataprovider tempdataprovider) {// The Information idictionary <string, Object> providerdictionary = tempdataprovider. loadtempdata (controllercontext); _ DATA = (providerdictionary! = NULL )? New Dictionary <string, Object> (providerdictionary, stringcomparer. ordinalignorecase): new dictionary <string, Object> (stringcomparer. ordinalignorecase); _ initialkeys = new hashset <string> (_ data. keys, stringcomparer. ordinalignorecase); _ retainedkeys. clear ();} public void save (controllercontext, itempdataprovider tempdataprovider) {// keystokeep = _ initialkeys + _ retainedkeys string [] keystokeep = _ initialkeys. union (_ retainedkeys, stringcomparer. ordinalignorecase ). toarray (); // keystoremove = _ data-keystokeep string [] keystoremove = _ data. keys. counter T (keystokeep, stringcomparer. ordinalignorecase ). toarray (); // except the used and unretained keys foreach (string key in keystoremove) {_ data. remove (key);} // save unused tempdata to tempdataprovider. savetempdata (controllercontext, _ data);} public object this [String key] {get {object value; If (trygetvalue (Key, out value )) {// The primary key is used when the primary key is used for saving. remove (key); return value;} return NULL;} set {_ DATA [Key] = value; _ initialkeys. add (key) ;}} public void keep (string key) {// reserve key _ retainedkeys. add (key );}

Note:

I once thought about a provider where the data is stored in httpcontext. items, because my temporary temp information disappears after the end of a request, but the members of the case may think too much of this problem, for example.

// The Custom tempdataprovider cannot be modified by using the settings (at least I did not find it). You can only use the scripts to override createtempdataprovider to use the public class mycontrollerbase in the system: controller {protected override itempdataprovider createtempdataprovider () {return New httpcontextitemstempdataprovider () ;}// use public class homecontrollerbase: mycontrollerbase {}

 

Additional information

    • [Codeplex] ASP. NET MVC
    • [Msdn] ASP. net mvc 2

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.