What if you need to maintain a state, either from one component to the other in the same request, or across requests? Web applications have traditionally offered solutions for this. Let's explore the options we have.
Using the request
Any object that you store in the request (in memory) will be available throughout its duration. Items are a strongly typed dictionary in the HttpContext class:
this.HttpContext.Items["timestamp"] = DateTime.UtcNow;
You can check for the existence of the item before accessing it; it is worth noting that the following is case sensitive:
if (this.HttpContext.Items.ContainsKey("timestamp")) { ... }
Of course, you can also remove an item:
this.HttpContext.Items.Remove("timestamp");
Using form data
The Form collection keeps track of all values submitted by an HTML FORM, normally after a POST request. To access it, you use the...