asp. net multiple ways to transfer data between different pages

  • 2020-11-20 06:03:26
  • OfStack

1. Get(that is, explicitly passed using QueryString)
Method: url is followed by the parameter.
Features: Simple and convenient.
Disadvantages: String length up to 255 characters; Data leak in url.
Applicable data: simple, small, critical data.
Scope of application: pass to yourself, pass to another target page; Often used to pass data between two pages.
Usage: for example: url followed by? UserID =... , to the target page, which can be accessed on the server using Request.QueryString ["InputText"] for its specified parameter values.

2. Post
Mode: The general mode. Submit with form.
Characteristics: The most commonly used method. The common trick is to store the hidden data in a hidden field for form to commit.
Applicable data: large amount of data, including file upload.
Scope of application: same as Get method
Usage: submit and ES32en. Transfer(url) are submitted in the server of ES30en. net after client form specifies action target; Obtained on the server using ES35en.Form ["FormFieldID"].

3. Properties of page objects
Method: asp.net special method. Use HttpContext to get information about the requested page.
Features: Objects can be stored directly.
Disadvantages: Note that the cast type must be correct.
Applicable data: all kinds of data.
Scope: Passing complex data between pages.
Usage: Get an instance of Context.Handler turning it into the requested page, then you can easily access the fields and even the properties in its form. Context refers to the HttpContext object, Handler is its attribute, Context.Handler means to create an instance variable of the source page class, and once the instance is obtained, the attributes and Public methods can be accessed directly.

4. cookie
Method: The classic method of storing data on the client.
Disadvantages: low security, limited by client Settings, only 20 cookie per site, 4096 bytes each.
Expiration date: custom or cleared by user
Applicable data: session data of the user (1 generally user name, user personalization information, etc.)
Scope of application: single user, all pages of the whole site
Usage: Reference Request.Cookies (to read information), Response.Cookies (to write information), or create Cookie objects directly with HttpCookieCollection.

5. Session
Mode: Store user data on the server.
Features: asp. net can set the storage mode and location of session, and whether the storage of SessionID depends on cookie.
Objects can be stored directly.
Disadvantages: asp.net has potential failure
Validity period: user activity time + custom delay.
Applicable data: User specific information.
Scope of application: single user, all pages of the whole site.
Usage: Session["CollectionName"] = value/object;

6. Cache
Mode: Store user data in the server data cache.
Features: Can greatly improve efficiency. Objects can be stored directly.
Disadvantages: Data is not updated timely.
Expiration date: Application life cycle or customization.
Applicable data: data that can be Shared by all pages and all users.
Usage: Cache["CollectionName"] = value|object;

7. Appliction
Mode: Store data here, equivalent to global variables.
Features: Objects can be stored directly. Shared data across the site.
Expiration date: Application life cycle.
Applicable data: data Shared by all pages and all users.
Usage: Appliction["CollectionName"] = value|object;

8. ViewState
Mode: ES125en.ES126en has a special mechanism for restoring page state.
Features: Serializes page controls and their stored data into a hidden field with name as _ViewState.
Disadvantages: In HTML, the security is low. Encryption and authentication can be set up, but the amount of data increases and the efficiency has an impact.
Applicable data: page PostBack needs to save the data, the data is too big to affect the efficiency of page sending.
Scope of application: Data saving of page itself.
Usage: ViewState["CollectionName"] = value;

9. Static
Way: Store data in static variables.
Features: Conducive to improving efficiency.
Disadvantages: if not good use will cause users or page data disorder, causing great hidden trouble. It is recommended to assign this value only once, and it is absolutely forbidden to change this value for a single user.
Applicable data: Data Shared by all users.
Scope of application: All page instances of this class.
Usage: Declare static variables in class.
Finish:
ViewState,static variables are available to save the data on the page itself.
Transfer data between pages is commonly used get,post,HttpContext, of course, can be adapted to 1 for their own data storage.
session and cookie are commonly used to share all page data of a single user, but can also be applied in the above two cases.
Common Appliction,cache,static variables are used for data sharing of the entire application (pages used by all users).

Conclusion:
Of course you can be flexible and use it to solve the problem, but choose the right one. Get and Session are commonly abused.
It is recommended to specify the set to be taken when using Request, for example, the parameters passed by Get method are taken from QueryString set,Post from Form, and cookie from Cookies. Although the Request[] set is used, all of the above can be obtained, but it causes a waste of performance. Originally it was directly in the Form set, but it traversed the QueryString,Form,Cookies and other sets before it was taken out.

ps: In the computer world, the one party that provides the service is called the server (server) and the other party that receives the service is called the client (client).


Related articles: