ASP.NET : Stick to the Context

Process
Process

ASP.NET : Stick to the Context

Welcome to CodeSpread!

What we are talking about?

We are talking about HttpContext Class. It is a part of System.Web namespace/assembly and is usually referred by,


System.Object
System.Web.HttpContext

What is its significance?

We know that HTTP has a stateless nature.When a user requests for a web application, the browser sends many independent requests to the server for the resources. ASP.NET has to maintain information about the the current HTTP request,running application, user session and many more processes, and finally blend them together to provide a personal experience to the user.

How a seamless experience is achieved?

To build a context, we require few classes which we are already using to maintain the state of the application. Then, All this context-related objects are blend into an extremely handy intrinsic object called Context.

Few of the objects are,

Application: A key/value pair collection of values that provides access to application-wide methods and events for all sessions..

Cache: The ASP.NET Cache object, which provides programmatic access to the cache.

Items: A key-value pair collection to store information and can be used to pass information between components.For example, the same piece of information can be used in Global.asax, in ASPX page, in the user controls within the page, and by the business logic the page calls.Context.Items as a state mechanism for very short-lived values.

Request: Information about the HTTP request, including browser information, cookies, request headers, cookies, client certificate, query string and many more.

Response: Provides access to the output stream for the current page. It is regularly required to write the cookie information.

Server: Exposes several useful helper methods, including Server.Execute(),Server.MapPath(), and Server.HtmlEncode(). This object is mainly used to transfer control between pages.

Session: A key/value pair collection of values that provides information to the current user session of the application.

User: The security context of the current user, if authenticated. Context.User.Identity is the user’s name.

Example:

Note-HttpContext.Current is a static property that is used to deliver the context for the current request.


// current context

HttpContext _context = HttpContext.Current;

// retrieve Session value

object _value = _context.Session["Key"];

Closing Note:

We have now two ways to access Request object.

HttpContext.Current.Request and Request directly from the base class.

Best part is they both behave the same way, but the optimal performance is provided by Direct Request object as there is no overhead of calling HttpContext object.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Maintain Page State on Browser’s Back Button ClickMaintain Page State on Browser’s Back Button ClickThis is a very common requirement and a major expectation for a quality user experience. Scenario: User visits a site which heavily uses AJAX. Here, AJAX will not let the user navigate from the p...
  • Don’t Mess with App_OfflineDon’t Mess with App_OfflineOne of the most useful features of ASP.NET is the "App_Offline.htm" . It provides a much easy way to make the ASP.NET application offline while we do our modifications on the site. How we do it? ...
  • 19 Dec: Must Read Codes [1-5]19 Dec: Must Read Codes [1-5]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn't require a full length descriptive article so combining them into groups of five. CodeRe...
  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their ...
  • How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010? 1. Open VS2010. 2. Select File -> New Project. 3. Select Visual C# -> Web -> ASP.NET MVC 2 Empty Web Application. Name it as MyMvcApplicat...
  • C#: Useful JSON in .NetC#: Useful JSON in .NetWhy JSON? All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from databas...
  • Few lines about Static code analysisFew lines about Static code analysisStatic code analysis We divide the term into two parts: Static+code analysis. Static in programming can be referred to as non-running/non-dynamic and code analysis is a process where the code is...
  • Caching is an evilCaching is an evilI know 'Caching' was created to ease out the network resources but i have seen the situations where it emerged as the biggest evil. I can remember many situations when the website is updated but...