Understand HttpContext class in ASP.NET C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 16824 red flag

Various properties of HttpContext class.

Understand HttpContext class in ASP.NET C#

In this tutorial we will try to understand context class in ASP.NET application. HttpContext encapsulates all HTTP-specific information about an individual HTTP request.  In below example we will demonstrate how to access and display property of HttpContext object. The context of the current HTTP request is accessed by using the Context property of the Page object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication
{
    public partial class TestApp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("Session ID of Application:-  " +  Context.Session.SessionID + "</br>");
            Response.Write("Number of Session :- " + Context.Session.Count + "</br>");
            Response.Write("Timestamp of current request :- " +  Context.Timestamp.ToString() + "</br>");
            Response.Write("Trace Enabled :- " +   Context.Trace.IsEnabled.ToString() + "<br/>");
            Response.Write("Is debugging enabled :- " +  Context.IsDebuggingEnabled.ToString() + "<br/>");
            Response.Write("Is custom errors enabled :- " + Context.IsCustomErrorEnabled.ToString() + "<br/>");
            Response.Write("");
 
 
        }
    }
}

Here is sample output.

 

Session ID

We can get session ID of current session using SessionID property of Session class. Here is sample code.

Context.Session.SessionID

Session is nothing but sequence of character(32 bit) which is unique for current session. When user gets log in one 

session ID get assign to user. Using this session ID user can able to exchange data between serer and client.

Number of Sessions

Number of session represents how many session objects have created in this current context. Here we did not define any session variable that’s why its showing 0. Means there is no session object in current session. If we create one session variable using below code

Session[“Session”]=”Value”;

Then the output will be 1

 

Trace Enable

This property will tell us that whether Tracing is enable or not in current application. It’s showing False because Tracing is not enable in current context.

 Debugging

IsDubbingEnable property will show thether debugging is enable or not in this application. It’s show True means debugging is enable. This is the content of web.config file

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
Custom Error Enable

It’s showing False because we did not enable custom error in web.config file.

Conclusion

In this article we have learned few properties of HttpContext class. Those properties are very useful to track data of current HTTP context.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)