Asp.net Method for saving Variables

Source: Internet
Author: User

ASP. NET status saving can be divided into client saving and server saving:

Using Client options to store page information without using server resources these options often have the lowest security but the fastest server performance, because the requirements for server resources are moderate. However, because the information must be sent to the client for storage, there are certain objective restrictions on how much information can be stored in this way.

The client can be stored in the following four ways:

1 viewstate:

Usage scenario: the value is automatically retained between multiple requests on the same page. It is mostly used for client events ., Typical scenarios include page information resetting, logon error count statistics, and grid column sorting.

Advantage: it is simple and relatively secure without using server resources because it is hashed and compressed and encoded for Unicode implementation.

Disadvantage: Because viewstate is stored on the page itself, large values cannot be stored. In addition, you can see the values in the source file. Although the values are hashed and compressed, there is still a risk of tampering.

Types that can be stored: String, integer, bool, array, arr aylist, hashtable, and Other types that can be serialized.

2 hiddenfield:

The usage scenario is: store a small amount of information that is frequently changed on the page and use it with client scripts. The typical usage scenario is: send back to the server after the client passes a series of verification, the server obtains the value from the client hiddenfield for processing. For example, in leyserhomepage, to delete a product, you need to pop up the confirmation form on the client. After the user confirms the form, the postbac K will return to the server side for the database delete operation. When the user confirms that the product is to be deleted, store the product ID to be deleted in a hiddenfield, and then execute Form (0 ). the submit is sent back to the server, and the server obtains the product ID value from the hiddenfield for database operations.

Advantages: simple implementation without using server resources, widely supported

Disadvantage: the security is not high, because it is included in the page for sending, so you can see its content through the source file. The storage structure is small. It only supports simple data structures such as string, integer, bool, array, and arraylis T. In addition, only simple single values are stored. To store multiple values, additional encoding is required. The storage capacity is small because it is stored on the page itself, so it cannot store large values. In addition, a large amount of data is blocked by firewalls and proxies. Note: After using hiddenfield, you need to send it back to the server for processing. You should use the http post method instead of the http get method (access through URL requests)

3 COOKIE:

Usage scenario: store the information that is frequently changed in a small number of pages. Typical usage scenarios are: Save the login user name for the logged-in website, which is convenient for user input, you can also save your personalized settings on custom projects.

Advantage: it is easy to implement without using server resources and you can configure the expiration time.

Disadvantage: The size is limited. Generally, the maximum cookie size supported by browsers is 4096 bytes. Client users may be configured to reject cookies. Security: the information stored on the client may be modified or obtained by malicious users, so sensitive information should not be saved. Persistence: the storage duration is affected by the client configuration.

Cookies are usually used to access the personalization of known user-defined content. In most cases, cookies are used as "identifiers" rather than "Identity Authentication". Therefore, only the user name, account name, or unique user ID (such as guid) is stored in cookies) it is sufficient to use it to access the user's personalized structure of the site.

4 querystring:

Use Case: the easiest way to pass information from one page to another.

Advantage: it does not use server resources and is widely supported and easy to implement.

Disadvantage: security, because the URL is directly exposed to the user, there is a risk of tampering. Limited capacity. Generally, browsers have 255 characters. You can only query strings on the URL request page. The query string cannot be read from the page that has been submitted to the server.

 

View status: you need to store a small amount of information for the pages that will be sent back to you. The use of the viewstate attribute provides basic security functions.

Hidden domain: you need to store a small amount of information for pages that are sent back to you or another page without high security. Client events can only use hidden domains on pages submitted to the server.

COOKIE: a small amount of information needs to be stored on the client without high security. (Personalized)

Query string: transfers a small amount of information from one page to another without high security. (Page Jump) a query string can be used only when one page is requested or another page is requested through a link.
There are three storage methods on the server:

The server-side options that store page information are often more secure than those of the client-side options, but they may use more web server resources, which may cause scalability when the information storage volume is large.

1 application:

Usage scenario: All requests require some common resources, which are obtained first by the first request and shared. Other requests do not need to waste resources for further retrieval. Typical scenarios: a stock market web site may obtain a large amount of financial stock information (maybe 40 MB of data) from the database every 5 minutes a day ), the information is then cached in the application state, so that all future search requests can access the information in the application state. The result greatly improves the performance of each request, because the incoming request does not require a round-trip process across processes, computers, or databases.

'Request FOR THE FIRST HTTP GET

If not me. ispostback then

'First, determine whether the application has been stored

If application. Item ("Database") is nothing then

'If not, retrieve the token from datacenter

Dim ds as new dataset

DS. readxml (server. mappath ("testdata. xml "))

Dim DV as new dataview (Ds. Tables (0 ))

'After the token is obtained, the token is saved to the application, so that other processes can use http get.

Application. Add ("Database", DV)

End if

Dim dv1 as dataview

Dv1 = application. Item ("Database ")

'Wait to the session to facilitate the http post request of the current process.

Session. Add ("Database", dv1)

End if

Advantages: easy to implement and global scope.

Disadvantage: persistence. If the server-side process that stores data is forcibly disabled, the data will be lost. Therefore, the application must have a minimum protection policy. If yes, it will be used, if you don't have one, take it by yourself. The server memory is consumed.

Note: because the data in the application is shared by multiple processes, you must use the following statement to update the value of the application to achieve exclusive update:

Application. Lock ()

Application. Item ("Database") = newdatabase

Application. Unlock ()

2 Session:

Usage scenario: a single process is used internally to store short-term and sensitive data for a single session.

Advantages: easy to implement and persistent. It can cope with IIS restart and secondary process restart. It can be used in multiple processes.

Disadvantage: memory consumption on the server side.

3 database:

Advantage: security. Capacity. Durability. Reliability and data integrity. Accessibility. Widely supported.

Disadvantages: complexity and Performance

Usage scenarios

The application state storage stores the globally changed information that is used by multiple users. Security is not a problem at this time. Do not store a large amount of information in the application state.

Session status stores short-term information specific to individual sessions and requires high security. Do not store a large amount of information in the session state. In applications that support many users, this may occupy a large amount of server resources and affect scalability.

The Database supports storing a large amount of information, managing transactions, or information must be able to withstand application and session restart. Data Mining is very important and requires high security.

 

From: http://www.cnsdn.com.cn/blog/article.asp? Id = 2163

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.