Asp.net 2.0 Status Management Summary

Source: Internet
Author: User

Recently, in the development process, I want to use the Asp.net status management mechanism to save some data. However, due to improper understanding of the data, many errors are caused.
I have read some documents and summarized them. I hope to deepen my impression.

The so-called status management is simply to share information between pages and between requests. Cookie, session, cache, application, viewstate, and profile may be used for State management and information data sharing. However, the differences between them and their use cases are still different. I will briefly describe what I have understood and used.
 
COOKIE: String stored on the client side. Note that only strings can be stored, and the size is limited: 4 K. The absolute Time Expiration Policy is used to save user login information. For example, csdn does this: it allows you to save cookies for two weeks. Within this week, your login information is saved in your computer, each time you open the site, the system will read the cookie. If it is not empty, log on directly.
Because it is stored on the client, security cannot be guaranteed.

Session: three storage methods are available: inproc, StateServer, and sqlserver. inproc is the default method. information is stored in the server memory. It can store any type of data without size restrictions (if the information is too large, it is easy to lose ). In StateServer mode, you need to enable the Asp.net Status Service. information is stored in the memory of the server where the Windows service is located and only serializable information can be stored (if it is a class, you need to add serializable tags ), no size limit. In SQL server mode, information is stored in SQL Server databases. Of course, you must install the aspnetdb database (which appears in sqlexpress). You can only store serialized information without the size limit ....
After talking about this, what can a session do? It is usually used for transferring page values or sharing information between events. The session is for every user, which is different from the cache. If you want to use the query result again in another request, you need to consider the session to save the last query result, instead of getting it from the database again.
The default expiration time of the exception session is 20 minutes. Of course, it can be configured in Web. config, but it does not seem to be too long.

Cache: any type can be stored in the server memory without the size limit. However, depending on the server configuration, the cache performance is also different. Because the cache is a memory object, there may be a loss problem under some circumstances, there is no good solution, as long as the system resources are tight, the cache may be cleared. This is why data stored in the cache is often lost. Therefore, when using this function, first judge whether the cache is null. If it is null, extract data from the member data table and put it into the cache, and then use the cache.
In addition, cache is generally used for updates that are not frequent, but frequently accessed data, such as rankings. As mentioned earlier, cache is a global object and is open to the entire application. Therefore, you cannot use it to store the results of a user's operations (I have made this mistake ).
Cache expiration policies are rich: never expire, absolute time expired, and smooth time expired (expired if no operation is performed within a certain period of time), associated expiration (such as associating a file, if the file is changed, the cache expires. Of course, for the expiration Policy, the cache update can also be performed in two ways: Active callback or passive access ..

Application: Application level. It is valid when the application starts. It ends when the application is closed, and there is no expiration Policy (it is always valid if the application is not closed ), information is stored in the web server memory and visible to all users.
Generally, it is difficult to control the manufacturing industry in the event of a global. asax file. Since each user can access it, you should lock it before access, and unlock the access to prevent concurrency. (The cache can handle concurrency by itself ).
The most common purpose is to count the website traffic.

Viewstate: in a single page of the Information Storage client, the page is located; when the page is closed, it becomes invalid. viewstate will participate in data sending back and forth when saving client information. Therefore, if the storage information is too large, it will make it difficult to open the page,
Viewstate is applicable to a single user and can only store serializable information.
Generally, it is very effective for scenarios where the information is small and the information on a single page is saved.

Profile: it is usually used to save users' personalized settings. available on the web. configure the attributes of the profile in config, which can be directly accessed on the server end. Then, different users can set different profile attributes for each user (either a logon user or an anonymous user) set a unique ID, store it in the cookie, and save the ID and the Set personalized information in the database (the default is aspnetdb in sqlexpress ). then, the user logs on to the same page again, the profile reads the ID from the cookie, and finds matching personalized information in the database based on the ID to load.
It can be seen that the profile information is stored in cookies and databases, and the cookie is valid. It is suitable for personalized settings for individual users. serialized information can be stored, and the size is limited by the database. Security is good, but every time information is loaded from the database, the performance will be affected.


In addition to the above status management policies, we often use URLs for page value passing. In this case, querystring is used. The usage is not described much, but note that for some special characters (@ # $), you must use httputility to pass them correctly. urlencode (s) is URL encoded, and then httputility is used. urldecode (); URL Decoding can be obtained correctly.

Asp.net 2.0 also provides a "cross-Page Submission" function: You can directly access the data of controls on the previous page on the target page. However, pay attention to the postbackurl application:

<Asp: button id = "btncrosspage" runat = "server" postbackurl = "crosspage. aspx" text = "cross-Page Submission" onclick = "btncrosspage_click"/>

In the load event of the crosspage page:

Protected void page_load (Object sender, eventargs E)
...{
String S = (previouspage. findcontrol ("txtname") as textbox). text;
}

In addition, you can access the methods and attributes of the source page on the target page.
1. Add a declaration to the source page in the header of the target page:
<% @ Previouspagetype virtualpath = "~ /Default. aspx "%>
2. Some public methods and attributes can be written in default. aspx on the Source Page.
Private string pagename;

Public String pagename
{
Get {return pagename ;}
Set {pagename = value ;}
}
Public String test ()
{
Return "Hello World ";
// Clientscript. registerstartupscript (GetType (), null, "alert ('OK');", true );
}
3. On the target page:
Label LBL = previouspage. findcontrol ("LBL") as label;
String result = previouspage. pagename;
String methodresult = previouspage. Test ();
Response. Write (LBL. Text + "--" + Result + "--" + methodresult );

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.