Introduction
State Management is the process by which the state and information of a page can be maintained over multiple requests.
ASP.NET provides several techniques to maintain the information. The choice depends on the scope of the data and performance considerations. One or more techniques can be used a single application depending on the needs
Various methods are used for State Management. They are
- View State
- Cookies
- Query String
- Session State
- Application State
All the preceding methods are used for State Management. View State, Cookies, and Query String are methods of Client-Side State Management whereas Session State and Application State are methods of Server-Side State Management.
ASP.NET state management can be broadly divided into the following following two categories
- Client-Side State Management
- Server-Side State Management
Client-side state management
In Client-Side State Management, the state of the page is maintained on the client side. The following are the various techniques for Client-Side State Management.
- View State
- Cookies
- Query String
In Client-Side State Management, data is stored on the client side and sent to the browser every time. This increases bandwidth usage. If the size of data is greater then the application will be less responsive. Performance issues would occur.
View state
- This is the Client-side State Method. Due to the stateless nature of the HTTP protocol, a page can't store any information for itself. View State is a method used to store information related to a page or its variables, and so on for feature use. In View State, the information is stored in an encoded manner.
- When processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden field. When the page is posted back to the server, the page parses the view state string at page initialization and restores property information on the page.
- In other words, View State is a technique to persist data through server round trips on a single page.
- View State uses a hidden field when the form is posted to store the state of a client.
- Each time a page is posted to itself, the contents of the _VIEWSTATE field are sent as part of the post.
Laminations of view state
- View State can be used with a single page.
- Since it stores information as a hidden field, it can be seen in the source code in the browser. Hence it is not a secure way.
- The performance issues arise with the increase in the size of the data stored as View State.
Cookies
A cookie is another Client-Side State Management technique to store data and access it on another page. It is actually a small file stored in the client's RAM or on the hard disk. Cookies can be either temporary or permanent.
- Temporary cookies: Temporary Cookies are stored in the client's RAM. It is transferred with a request and a response. Internally all cookies are temporary. Consider a small example. If a default.aspx requests default2.aspx and cookies are sent then how does the web server know that the request is from the same client? Actually, on the first request from the client, a session ID is given. A Session ID is nothing but a series of hits by the same client. This session ID is stored in the client's RAM and the next time the request comes, it checks for the file, and considering the same session ID, the page is rendered.
- Permanent cookies: Permanent Cookies are stored as a file on the client's hard disk. It is stored in a C:\Documents and Settings\Cookies folder. Permanent Cookies are created by setting the Expires property.
For example. ck.Expires Date and Time.NOW.AddSeconds(20);
Cookies store a maximum of 4KB of information.
Laminations of cookies
- It is a small textual file so it can store a small amount of data.
- It is not secure since it is stored on the client side and can be tempered. For example, risk of username/password is being stored on the client machine.
- In some cases, the browser blocks the cookies.
Query string
- The query string is also a Client-Side State Management technique. It is the information that is appended to the URL.
- A Query string can be viewed in the address bar of the browser.
- The query string starts with a question mark (?) and an attribute/value pair, called “category”.
- A Query string provides a simple way to maintain state information. For example, they are an easy way to pass information from one page to another, such as passing a product number from one page to another page where it will be processed.
- The page is submitted using an HTTP GET command for query string values to be available during page processing.
- Information passed in a query string cannot be used to send sensitive data since a user can send a URL to other users and pass the information.
Laminations of the query string
- It is not a secure way since it can be read and modified in the address bar of the browser.
- Only a small amount of data can be transferred.
- Some browsers also limit the length of a URL.
Server-side state management
In the Server-Side State Management, the state of the page is maintained at the server side. The following are the various techniques for Server-Side State Management.
- Application State (Variable)
- Session State (Variable)
- SQL Server database
Server-side state Management provides better security. The state is saved on the server and isn't delivered to the client. The server side reduces the traffic to and from the client because the data is not sent to the browser.
Application state (variable)
- Application state is a global storage mechanism that saves data that can be accessed from every web page in a web application. The data that is saved in the application variable is maintained between every request that is sent to the server.
- The application state is maintained in the HTTP Application class implemented as a directory. The application variable holds data that is global for the web application and is not associated with a specific user. Placing the data in the application state gives you a single access point for shared application data. The data that you save in the application state isn't permanent and is cleaned whenever the application restarts.
- It should be noted that many users might be accessing the application state simultaneously so any call to the application state object needs to be thread-safe. This can be easily done in ASP.NET using the lock keyword on the statements that are accessing application state objects.
Session state (variable)
- A session is a series of requests made by the client. A new session starts when a user sends the first request to the web application. This means that every user of the web application will have a separate session state. The session state is used to store and retrieve information about the user as the user navigates from one page to another page in ASP.NET web applications.
- The session has a timeout period and after the timeout the session data is lost and a new session is created for the user. The timeout occurs when the user is no longer active for some time. The session object is implemented as a key/value dictionary that is represented by an instance of the HTTP Session object.
- The session is used for the following scenarios.
- To track user sessions.
- Rise session management event.
- Store session-specific data.
- Session objects are one of the intrinsic objects supported by ASP.NET. They provide a developer with a complete web session management solution.