DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • The SPACE Framework for Developer Productivity
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  • Using Render Log Streams to Log to Papertrail

Trending

  • The SPACE Framework for Developer Productivity
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  • Using Render Log Streams to Log to Papertrail

HttpContext.Items[] vs Session[]

Dave Bush user avatar by
Dave Bush
·
Oct. 12, 09 · News
Like (0)
Save
Tweet
Share
10.84K Views

Join the DZone community and get the full member experience.

Join For Free

Since .NET first because available, passing data around during a request has become a lot easier.  The ability to set a property has made that so.  Still there are times when setting a property just won’t do the trick.

One such time is getting data from the middle tier back up to the view separate from a DataBinding operation.  That is, you databind a control to the middle tier and that method needs to set a value that will be used elsewhere in the view.  Not in the item that is being bound.

The natural, obvious, tendency is to set a session variable.  But, there is a better way.

The problem with session variables is that they have to be cleaned up manually or they will hang around longer than we actually need them.  This will use up more session memory than is required and can potentially cause side effects that will be difficult to debug.

Instead you can use the Items[] collection that is part of the HttpContext class.  It works the same as a session variable, but it only hangs around for the duration of the request.  Once the information is sent back to the browser, the variables that were set in the Items[] collection go away.

You might set your variable in the middle tier like this:

HttpContext.Current.Items["myVar"] = "Some Data Here";

And retrieve it later like this:

string myVar = (string)(HttpContext.Current.Items["myVar"]);

 

Session (web analytics)

Published at DZone with permission of Dave Bush, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • The SPACE Framework for Developer Productivity
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  • Using Render Log Streams to Log to Papertrail

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: