.netCoders Contact Us
Search:

Intrinsic Objects

The exam also covers the use of the ASP.NET intrinsic objects. These objects are:
  • Response
  • Request
  • Session
  • Server
  • Application

Response

The Response object is an instance of System.Web.HttpResponse, and is used for returning information to the browser. Using this object, for example, you can write HTML with the Write() method, set cookies (Response.Cookies.Add()), and issue redirects (Response.Redirect()). The following code snippet shows the Response object in use:
Response.Write("Hello World!");
Response.Buffer = true;
Response.Cookies.Add(new HttpCookie("name", "james"));
Response.Redirect("userhome.aspx");

Request

The Request object is an instance of System.Web.HttpRequest. It is used to obtain information sent by the client's browser, including cookies and form variables. The following code snippet shows the request object in use:
string strProdName = Request.QueryString["prodname"].ToString();
string strFName = Request.Cookies["name"].ToString();
foreach(string s in Request.ServerVariables.Keys)
{
    Response.Write(s + " = " + Request.ServerVariables[s]);
}

Session

The Session object is an instance of System.Web.SessionState.HttpSessionState, and encapsulates the state management functionality provided by the .NET runtime. Using this object, you can add and retrieve objects for a user's session. The following code snippet shows the Session object in use:
//Add Specific Variable
Session["firstname"] = "James";

//Retrieve Specific Variable
string strName = Session["firstname"].ToString();

//Loop through all Variables
foreach(string key in Session.Keys)
{
    Response.Write(key + " = " + Session[key]);
}
For more information, see our section on Session State Management.

Server

The Server object is available for backwards compatibility, and is an instance of System.Web.HttpServerUtility. This object supports the methods of the classic ASP Server object, including CreateObject for instantiating COM objects, MapPath for converting virtual paths to physical paths, and HtmlEncode for converting strings to HTML. The following code snippet shows the Server object in use:
string strPath = Server.MapPath("/membersonly/disclaimer.txt");
object o = Server.CreateObject("DotnetCoders.FinanceVB");
Response.Write(Server.HtmlEncode("<h1> is for headings"));

Application

The Application object is an instance of System.Web.HttpApplicationState, and encapsulates application-wide state functionality. This object has a dictionary interface, allowing you to add and retrieve objects from any page within the ASP.NET application. The following code snippet shows the Application object in use:
//Add value
Application.Add("LastVisitTime", DateTime.Now);

//Display all values
foreach(string s in Application.Keys)
{
    Response.Write(s + " = " + Application[s]);
}