Http context and its members
This resource provides an introduction of Http Context object along with the sample codes for all the members of HttpContext object such as Request, Resoponse, Server, Session, Application, Cache, Profile and Timespamp in a single console application.
HttpContext
Introduction:
Whenever web server receives a request for asp.net resource (either web page or service), an instance of System.Web.HttpContext is created.
HttpContext object has each and every information about the current request.
HttpContext object contains following information...
Intrinsic objects: Request, Response, Server, Session, Application, and Cache
User property: Information about the current user
Error property: Information about all errors
Item property: A hashtable, contains objects, participate in current web request. E.g. Webpage, User/Custom Controls
Following Code Snippet covers almost all the members of HttpContext object...
Code Snippets: (Create a console application with following code)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Get the HttpContext Object
System.Text.StringBuilder sb = new System.Text.StringBuilder();
HttpContext hc = HttpContext.Current;
string b = "<br/>";
#region retrieve various members from HttpContext object
// Request member
sb.Append(" Request.AcceptTypes count.." + hc.Request.AcceptTypes.Length + b);
sb.Append(" Request.ApplicationPath.. " + hc.Request.ApplicationPath + b);
sb.Append(" Request.AppRelativeCurrentExecutionFilePath.. " + hc.Request.AppRelativeCurrentExecutionFilePath + b);
sb.Append(" Request.Browser.Version.. " + hc.Request.Browser.Version + b);
sb.Append(" Request.Browser.Id.. " + hc.Request.Browser.Id + b);
sb.Append(" Request.Browser.Frames.. " + (hc.Request.Browser.Frames ? "Supportes" : "Un-Supported") + b);
sb.Append(" Request.Browser.Crawler.. " + (hc.Request.Browser.Crawler ? "Search Engine Crawler" : "Not Search Engine Crawler") + b);
sb.Append(" Request.Browser.Cookies.. " + (hc.Request.Browser.Cookies ? "Supported" : "Not supported") + b);
sb.Append(" Request.Browser.ClrVersion.. " + hc.Request.Browser.ClrVersion.ToString() + b);
sb.Append(" Request.Browser.Capabilities.Countn.. " + hc.Request.Browser.Capabilities.Count.ToString() + b);
sb.Append(" Request.Browser.Beta.. " + (hc.Request.Browser.Beta ? "Yes" : "No") + b);
sb.Append(" Request.Browser.BackgroundSounds.. " + (hc.Request.Browser.BackgroundSounds ? "Supported" : "Not supported") + b);
sb.Append(" Request.Browser.ActiveXControls.. " + (hc.Request.Browser.ActiveXControls ? "Supported" : "Not supported") + b);
sb.Append(" Request.ClientCertificate.Subject.. " + hc.Request.ClientCertificate.Subject + b);
sb.Append(" Request.ClientCertificate.Issuer.. " + hc.Request.ClientCertificate.Issuer + b);
sb.Append(" Request.ContentLength.. " + hc.Request.ContentLength.ToString() + b);
sb.Append(" Request.ContentType.. " + hc.Request.ContentType + b);
sb.Append(" Request.Cookies.Counte.. " + hc.Request.Cookies.Count.ToString() + b);
sb.Append(" Request.CurrentExecutionFilePath.. " + hc.Request.CurrentExecutionFilePath + b);
sb.Append(" Request.CurrentExecutionFilePathExtension.. " + hc.Request.CurrentExecutionFilePathExtension + b);
sb.Append(" Request.FilePath.. " + hc.Request.FilePath + b);
sb.Append(" Request.Files.Count.. " + hc.Request.Files.Count.ToString() + b);
sb.Append(" Request.Headers.Count.. " + hc.Request.Headers.Count.ToString() + b);
sb.Append(" Request.HttpMethod.. " + hc.Request.HttpMethod + b);
sb.Append(" Request.InputStream.Length.. " + hc.Request.InputStream.Length.ToString() + b);
sb.Append(" Request.InputStream.. " + hc.Request.InputStream.ToString() + b);
sb.Append(" Request.IsAuthenticated.. " + (hc.Request.IsAuthenticated ? "Yes" : "No") + b);
sb.Append(" Request.IsLocal.. " + (hc.Request.IsLocal ? "Yes" : "No") + b);
sb.Append(" Request.IsSecureConnection.. " + (hc.Request.IsSecureConnection ? "Yes" : "No") + b);
sb.Append(" Request.LogonUserIdentity.AuthenticationType.. " + hc.Request.LogonUserIdentity.AuthenticationType + b);
sb.Append(" Request.LogonUserIdentity.Groups[0].. " + hc.Request.LogonUserIdentity.Groups[0].ToString() + b);
sb.Append(" Request.LogonUserIdentity.ImpersonationLevel.. " + hc.Request.LogonUserIdentity.ImpersonationLevel.ToString() + b);
sb.Append(" Request.LogonUserIdentity.IsAnonymous.. " + (hc.Request.LogonUserIdentity.IsAnonymous ? "Yes" : "No") + b);
sb.Append(" Request.LogonUserIdentity.IsAuthenticated.. " + (hc.Request.LogonUserIdentity.IsAuthenticated ? "Yes" : "No") + b);
sb.Append(" Request.LogonUserIdentity.Name.. " + hc.Request.LogonUserIdentity.Name + b);
sb.Append(" Request.LogonUserIdentity.Owner.. " + hc.Request.LogonUserIdentity.Owner + b);
sb.Append(" Request.LogonUserIdentity.Token.. " + hc.Request.LogonUserIdentity.Token.ToString() + b);
sb.Append(" Request.LogonUserIdentity.User.AccountDomainSid.Value.. " + hc.Request.LogonUserIdentity.User.AccountDomainSid.Value+b);
sb.Append(" Request.LogonUserIdentity.User.Value.. " + hc.Request.LogonUserIdentity.User.Value + b);
sb.Append(" Request.Params.Counte.. " + hc.Request.Params.Count.ToString() + b);
sb.Append(" Request.Path.. " + hc.Request.Path + b);
sb.Append(" Request.PathInfo.. " + hc.Request.PathInfo + b);
sb.Append(" Request.PhysicalApplicationPath.. " + hc.Request.PhysicalApplicationPath + b);
sb.Append(" Request.PhysicalPath.. " + hc.Request.PhysicalPath + b);
sb.Append(" Request.QueryString.Count.. " + hc.Request.QueryString.Count.ToString() + b);
sb.Append(" Request.RawUrl.. " + hc.Request.RawUrl + b);
sb.Append(" Request.RequestContext.HttpContext.Items.Count.. " + hc.Request.RequestContext.HttpContext.Items.Count.ToString() + b);
sb.Append(" Request.RequestType .. " + hc.Request.RequestType + b);
sb.Append(" Request.ServerVariables.Count.. " + hc.Request.ServerVariables.Count.ToString() + b);
sb.Append(" Request.TotalBytes.. " + hc.Request.TotalBytes.ToString() + b);
sb.Append(" Request.Url.AbsolutePath.. " + hc.Request.Url.AbsolutePath + b);
sb.Append(" Request.Url.AbsoluteUri.. " + hc.Request.Url.AbsoluteUri + b);
sb.Append(" Request.Url.Authority.. " + hc.Request.Url.Authority + b);
sb.Append(" Request.Url.Fragment.. " + hc.Request.Url.Fragment + b);
sb.Append(" Request.Host.. " + hc.Request.Url.Host + b);
sb.Append(" Request.Url.HostNameType.. " + hc.Request.Url.HostNameType.ToString() + b);
sb.Append(" Request.Url.IsAbsoluteUri.. " + (hc.Request.Url.IsAbsoluteUri ? "Yes" : "No") + b);
sb.Append(" Request.Url.IsFile.. " + (hc.Request.Url.IsFile ? "Yes" : "No") + b);
sb.Append(" Request.Url.PathAndQuery.. " + hc.Request.Url.PathAndQuery + b);
sb.Append(" Request.Url.Port.. " + hc.Request.Url.Port.ToString() + b);
sb.Append(" Request.Url.Query.. " + hc.Request.Url.Query + b);
sb.Append(" Request.Url.Segments.Length.. " + hc.Request.Url.Segments.Length.ToString() + b);
sb.Append(" Request.Url.UserInfo.Length.. " + hc.Request.Url.UserInfo.Length.ToString() + b);
sb.Append(" Request.UserAgent.Length.. " + hc.Request.UserAgent.Length.ToString() + b);
sb.Append(" Request.UserHostAddress.. " + hc.Request.UserHostAddress + b);
sb.Append(" Request.UserHostName.. " + hc.Request.UserHostName + b);
sb.Append(" Request.UserLanguages.Length.. " + hc.Request.UserLanguages.Length.ToString() + b);
sb.Append(" Request.UserLanguages[0].. " + hc.Request.UserLanguages[0] + b);
// Resoponse member
sb.Append(" Response.Expires.. " + hc.Response.Expires.ToString() + b);
sb.Append(" Response.ExpiresAbsolute.ToLongDateString().. " + hc.Response.ExpiresAbsolute.ToLongDateString() + b);
sb.Append(" Response.RedirectLocation.. " + hc.Response.RedirectLocation + b);
sb.Append(" Response.StatusCode.. " + hc.Response.StatusCode.ToString() + b);
sb.Append(" Response.StatusDescription.. " + hc.Response.StatusDescription + b);
// Server member
sb.Append(" Server.MachineName.. " + hc.Server.MachineName + b);
sb.Append(" Server.ScriptTimeout.. " + hc.Server.ScriptTimeout.ToString() + b);
// Session member
sb.Append(" Session.CodePage.. " + hc.Session.CodePage.ToString() + b);
sb.Append(" Session.CookieMode.. " + hc.Session.CookieMode.ToString() + b);
sb.Append(" Session.Count.. " + hc.Session.Count.ToString() + b);
sb.Append(" Session.IsCookieless.. " + (hc.Session.IsCookieless ? "Yes" : "No") + b);
sb.Append(" Session.IsNewSession.. " + (hc.Session.IsNewSession ? "Yes" : "No") + b);
sb.Append(" Session.IsSynchronized.. " + (hc.Session.IsSynchronized ? "Yes" : "No") + b);
sb.Append(" Session.Keys.Count.. " + hc.Session.Keys.Count.ToString() + b);
sb.Append(" Session.LCID.. " + hc.Session.LCID.ToString() + b);
sb.Append(" Session.Mode.. " + hc.Session.Mode.ToString() + b);
sb.Append(" Session.SessionID.. " + hc.Session.SessionID + b);
sb.Append(" Session.StaticObjects.Count.. " + hc.Session.StaticObjects.Count.ToString() + b);
sb.Append(" Session.SyncRoot.. " + hc.Session.SyncRoot.ToString() + b);
sb.Append(" Session.TimeOut.. " + hc.Session.Timeout.ToString() + b);
// Application member
sb.Append(" Application.AllKeys.Length.. " + hc.Application.AllKeys.Length.ToString() + b);
sb.Append(" Application.Count.. " + hc.Application.Count.ToString() + b);
sb.Append(" Application.StaticObjects.Count.. " + hc.Application.StaticObjects.Count.ToString() + b);
// Cache member
sb.Append(" Cache.Count.. " + hc.Cache.Count.ToString() + b);
sb.Append(" Cache.EffectivePercentagePhysicalMemoryLimit.. " + hc.Cache.EffectivePercentagePhysicalMemoryLimit.ToString() + b);
sb.Append(" Cache.EffectivePrivateBytesLimit.. " + hc.Cache.EffectivePrivateBytesLimit.ToString() + b);
// Profile member
sb.Append(" Profile.UserName.. " + hc.Profile.UserName + b);
sb.Append(" Profile.PropertyValues.Count.. " + hc.Profile.PropertyValues.Count.ToString() + b);
// Timespamp member
sb.Append(" Timestamp.. " + hc.Timestamp.ToLongDateString() + b);
Response.Write(sb);
#endregion
}
}
}
Press F5 to test this application it.
Please click here to know the Output...
Hope this Code snippet covers all the members of HTTPContext object.
Thanks
Sharad