CSC378 ยท 2081 ยท 5 marks
Define state management. Explain the parts of state management on the client side.
Define state management. Explain the parts of state management on the client side. [5]
Quick answer
State management is the process of preserving and maintaining the state (data/information) of a user or application across multiple HTTP requests. Since HTTP is a stateless protocol, each request is treated as independent and the server does not automatically remember previous interactions. State management techniques are used to overcome this limitation and retain data between requests, either on the client side or the server side.
State Management in ASP.NET
Definition of State Management
ASP.NET Web Forms includes several options that help preserve data on both a per-page basis and an application-wide basis.
Client-Side State Management
In client-side state management, the state data is stored on the client's machine (browser) rather than on the server. This reduces server load but may have limitations in terms of security and data size.
The main parts of client-side state management are:
1. Cookies
Cookies store data directly in the user's browser. The browser automatically sends cookies with every HTTP request made to the server.
Key Points:
- Since cookies are sent with every request, their size should be kept to a minimum.
- Commonly used to personalize content for a known user, especially for identification without full authentication.
- Example: storing a user's name or preferred color theme.
Reading a Cookie:
// Read cookie from HttpContextAccessor
string cookieValueFromContext = HttpContextAccessor.HttpContext.Request.Cookies["key"];
// Read cookie from Request object
string cookieValueFromReq = Request.Cookies["key"];
Writing a Cookie:
public void SetCookie(string key, string value, int? expireTime)
{
CookieOptions option = new CookieOptions();
if (expireTime.HasValue)
option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
else
option.Expires = DateTime.Now.AddMilliseconds(10);
Response.Cookies.Append(key, value, option);
}
Removing a Cookie:
Response.Cookies.Delete(key);
2. Query Strings
A query string is a part of the URL that carries a limited amount of data from one request to another by appending it to the URL of the new request.
Key Points:
- Useful for capturing state in a persistent manner.
- Allows sharing of links with embedded state (e.g., bookmarking a filtered search result).
- Data is visible in the URL, so it is not suitable for sensitive information.
Example:
https://example.com/products?category=books&page=2
Here, category=books and page=2 are query string parameters that carry state between requests.
Summary Table
| Technique | Storage Location | Size Limit | Use Case |
|---|---|---|---|
| Cookies | Browser | Small | User preferences, personalization |
| Query Strings | URL | Very small | Page navigation, shareable links |
Both techniques are important client-side state management strategies in ASP.NET that help maintain user data across stateless HTTP requests without relying on server memory.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.