CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » HttpContext Object for Developers   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
 

Community News
No News Feeds available at this time.
 
HttpContext Object for Developers

By Satheesh babu
Posted On Apr 28,2008
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 7
Print this article.
Category: ASP.Net

Subscribe to our feed!

HttpContext Object for Developers

 

HttpContext object will hold information about the current http request. In detail, HttpContext object will be constructed newly for every request given to an ASP.Net application and this object will hold current request specific informations like Request, Response, Server, Session, Cache, User and etc. For every request, a new HttpContext object will be created which the ASP.Net runtime will use during the request processing. A new HttpContext object will be created at the beginning of a request and destroyed when the request is completed. To know more about the use of HttpContext object in ASP.Net request processing please follow the article attached in the reference section of this article. Also, this object can be very well used in our application to solve some of problems which I will discuss in this article.

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 Server.Transfer()

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 another.

 

Source Form

        Context.Items.Add("empID", "123456");

        Server.Transfer("Destination.aspx");

 

Destination Form

       string empID = Context.Items["empID"].ToString();

       Response.Write(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.

 

Source Form

        Employee emp = new Employee();

        emp.FirstName = "Satheesh";

        emp.LastName = "Babu";       

        Context.Items.Add ("emp", emp);

        Server.Transfer("Destination.aspx");

 

Destination Form

       Employee emp = Context.Items["emp"] as Employee;

       Response.Write(emp.FirstName+" " + emp.LastName);

 

Accessing Request/Response/Server/Session/Cache/User 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 HttpContext object.

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

{

       public AccessPageObjects()

       {

              //

              // TODO: Add constructor logic here

              //

       }

    public void Print()

    {

        HttpContext context = HttpContext.Current;

        context.Response.Write("Test");

        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.

 



ASP.Net Hosting

Recent Articles

Pass value to usercontrols using HttpContext object

We can pass values to usercontrols from codebehind using Context.Item Collection.

 

UserControl

public partial class WebUserControl : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)

    {

       Employee emp = Context.Items["emp"] as Employee;      

       Response.Write(emp.FirstName + " " + emp.LastName + " in user control.");

    }

}

 

The above requirement can also be achieved through Page.Item collections in the same way like above. But Context object will be really useful when we need to pass an object from a source page to destination page’s usercontrols.

 

Source Form

        Employee emp = new Employee();

        emp.FirstName = "Satheesh";

        emp.LastName = "Babu";       

        Context.Items.Add("emp", emp);

        Server.Transfer("Destination.aspx");

 

UserControl in Destination Page

public partial class WebUserControl : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)

    {

       Employee emp = Context.Items["emp"] as Employee;      

       Response.Write(emp.FirstName + " " + emp.LastName + " in user control.");

    }

}

 

ASP.Net Request Processing Time

A very good use of HttpContext object will be calculating the request processing time. Take a look at Rick’s article in West-Wind with the title “A low-level Look at the ASP.NET Architecture” where he explains it very neatly. Find the link in the reference section for a depth understanding in ASP.Net request processing. Request processing time can be calculated in Global.asax file through Application_BeginRequest and Application_EndRequest event. By default, this event will not be present in Global.asax file so we need to add it explicitly.

 

    protected void Application_BeginRequest(Object sender, EventArgs e)

    {

        Context.Items.Add("Request_Start_Time", DateTime.Now);

    }

    protected void Application_EndRequest(Object sender, EventArgs e)

    {

        TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);

    }

 

Reference

A low-level Look at the ASP.NET Architecture

HttpContext.Current

 

Conclusion

This article will help developers to understand the usage of HttpContext object at one place to achieve some of the handy things. Most of the tips discussed here use Item collection in the Context object to accomplish the tasks and this proves the importance of Item collection packed with HttpContext object. Thus, we can understand that HttpContext object is not only used by the ASP.Net runtime for request processing but also by developers.

Happy Coding!!

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments
    Nice Stuff
    Very Good for beginners and even for indeterminate
    http
    Good one article on http.
    Excellent
    Excellent Article
    Http
    this is good and easy to understand
    Really Good One
    Thanks for such a good article.
    Cool
    Clear Explanation
    thanx
    thanx for a great article