Cold Fusion Request Variable Type for ASP.NET

i know i’m late to the party most times but i want to share this with any ASP.Net web developers that will find this as useful as myself… and i find it very useful. i dev’d some Cold Fusion code back in the day… it was a job what can i say… anyhow i remember the coolest variable type ever that CF had.. it was the ‘request’ variable. not to be confused with the ASP and ASP.Net request object. in ColdFusion a request variable is just a generic variable that can be read and written too throughout the entire HTTP request thread… all without needed to be passed around like local variables and without the excessive memory overhead of session variables.

for the last several years while working in a .Net world i’ve had to settle on either explicit passing of local variables (yuck… pain to build for and maintain) or session variables (ugh… easy but you know you’re utilizing a lot more memory than you really need). but today the simplest thing dawned on me… the Page object is scoped very similarly to an HTTP request… and it has an Items collection… and you can read and write entire objects into the Items collection. best of all at the end of the Page’s lifecycle the Items collection is disposed of and the memory is free… free at last!

so there you go, if you’ve ever needed a Cold Fusion style request variable in ASP.Net use Page.Items.

it’s super easy, but here’s an example of setting and reading:

[VB]
‘ this creates the ‘item/variable’
Page.Items.Add(“Name”,”Value”)

‘ this is where you read it
‘ remember you can read from anywhere during
‘ the request (like from a user control)
Response.Write(Page.Items.Item(“Name”))

[C#]
// this creates the ‘item/variable’
Page.Items.Add(“Name”,”Value”)

// this is where you read it
// remember you can read from anywhere during
// the request (like from a user control)
Response.Write(Page.Items.Item[“Name”])