Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Application, Cookie, Session, Cache and ViewState in ASP.NET

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what is the use of Application, Cookie, Session, Cache and ViewState in ASP.NET, which can be used for reference. Interested friends can refer to it. I hope you can learn a lot after reading this article.

1. Application

Application provides access to application-wide methods and events for all sessions. It also provides access to application-wide caches that can be used to store information. Application state is a data repository that is available to all classes in an ASP.NET application. It is stored in the memory of the server, so it performs faster than storing and retrieving information in the database. Unlike session state specific to a single user session, application state is applied to all users and sessions. Therefore, the application state is ideal for storing common data that is small in quantity and does not change with the user.

The key features of Application are: stored in server memory, independent of users, that is, multi-user sharing, that is, it will not be actively discarded during the whole lifetime of the application, no serialization, and no server-client data transmission.

II. Cookie

Cookie provides a way to store user-specific information in Web applications. For example, when users visit your site, you can use Cookie to store user preferences or other information. When the user visits your site again, the application can retrieve previously stored information. When developers programmatically set up Cookie, they need to serialize the data they want to save into strings (and note that many browsers have a 4096-byte limit on Cookie) and then set it.

The key features of Cookie are: stored on the client hard disk, related to the user, persistent storage within a certain period of time, data can be shared across browsers, need to be serialized, and server-client data transfer occurs.

III. Session

Session provides information for the current user session. It also provides access to session-wide caches that can be used to store information, as well as methods to control how sessions are managed. Application state is a data repository that is available to all classes in an ASP.NET application. It is stored in the memory of the server, so it performs faster than storing and retrieving information in the database. Unlike application state, which is not specific to a single user session, session state is applied to individual users and sessions. Therefore, the application state is ideal for storing common data that is small in quantity and varies with users. And because there is no server-client data transfer, Session is also suitable for storing secure data about users, such as shopping cart information.

The key features of Session are: stored in server memory, related to the session, existing in the whole lifetime of the session will not be actively discarded, not serialized, no server-client data transmission occurs.

IV. Cache

ASP.NET provides you with a powerful, easy-to-use caching mechanism for storing objects that require a lot of server resources to create in memory. Caching these types of resources can greatly improve the performance of your application. It is stored in the memory of the server and allows you to customize how items are cached and how long they are cached. For example, when there is a lack of system memory, the cache automatically removes rarely used or lower-priority items to free memory. This technique, also known as cleanup, is one of the ways caching ensures that expired data does not use valuable server resources. It is not session-related, so it is shared by multiple sessions, so using it can improve site performance, but may disclose users' security information, and because Cache may be automatically removed when the server is out of memory, you need to detect whether the Cache entry still exists every time you get data.

The key features of Cache are: stored in the server memory, independent of the session, may be discarded at any time according to the status of the server memory resources, not serialized, no server-client data transmission occurs.

Here, I would also like to make a description of ViewState by the way, in order to compare with the above four ways of data persistence. Because, although ViewState cannot share data across pages, it can be used to retain values between multiple requests to the same page on the same page.

5. ViewState

The ViewState property provides a dictionary object that retains values between multiple requests to the same page. This is the default method used by pages to retain page and control property values between round trips. When a page is processed, the current state of the page and control is hashed into a string and saved in the page as one or more hidden fields (if the amount of data stored in the ViewState property exceeds the specified value in the MaxPageStateFieldLength property). When the page is posted back to the server, the page parses the view state string during the page initialization phase and restores the property information in the page. You can also use view state to store values. By default, ViewState is not encrypted and server-client data transfer occurs.

The key features of ViewState are: stored on the page, session-related and page-related, serialized, server-client transmission occurs by default, and is not encrypted by default.

As for the circumstances in which server-client transmission does not occur in ViewState, or is encrypted, we will talk about it in later chapters.

To sum up, we summarize some common and typical examples:

Shopping carts for e-commerce sites: use Session because shopping cart information is session-relevant and security is important.

The "remember me" function of forums or other sites: use Cookie, because it is often saved as only a user name, and the user needs to still exist the next time the user logs in.

Site counters: use Application if you don't use a database, because counters are session-independent. But even when using a database, I recommend that you use Application to save the count values at the same time, and then save them to the database at regular intervals, because this can reduce the number of visits to the database and improve performance.

Product information: Cache is the preferred choice, because product information is usually independent of the session, low frequency of modification and high frequency of access to the data, using Cache to save can effectively improve the performance of the website.

Finally, we give a table that lists the feature comparisons of the above data persistence methods so that you can make a decision:

Whether the ApplicationCacheSessionCookieViewState storage location server client will be actively discarded will not be related to the session whether it is serialized or not whether the server-client transfer is not encrypted (default) No (default) Thank you for reading this article carefully I hope the article "what is the use of Application, Cookie, Session, Cache and ViewState in ASP.NET" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report