One of the best features of HttpContext object is using
its Item Collection. This article will contemplate on some of the very useful
features that can be achieved through HttpContext Class.
Hide QueryString or Pass value with
Sometimes, we will have requirements where we need to
pass data from source form to destination form when using Server.Transfer()
method. Since, Server.Transfer will make no round trip to the client, the
destination page processing will also falls under the same request. This is one of the
ideal scenario where we can use the Item collection of HttpContext object.
Refer the below code where we are passing EmployeeID from one form to
string empID =
The above item collection accepts key value pair of type
Object. Hence, we have capability of passing objects from one form to another
with this item collection when we use Server.Transfer() method. The above tip also
prevents the QueryString or data that is passed to the destination page shown
to the user.
Employee emp = new Employee();
emp.FirstName = "Satheesh";
emp.LastName = "Babu";
Context.Items.Add ("emp", emp);
Employee emp = Context.Items["emp"] as Employee;
Response.Write(emp.FirstName+" " +
object in a Class
Most of us think the above objects can be accessed only in
CodeBehind file and it can’t be accessed outside codebehind class i.e. any other
custom class. But in reality, it can be accessed via the current request’s
How to get the HttpContext object of the current
request in a class file?
HttpContext class has a static property called Current
which gives us the reference of current request’s HttpContext object. Once we
have the HttpContext object we can access those objects easily.
public class AccessPageObjects
// TODO: Add constructor logic here
public void Print()
HttpContext context = HttpContext.Current;
context.Session["Key"] = "Value";
string user = context.User.Identity.Name;
For the above code to work we need to make sure that we
are including System.Web namespace in the class file.