Let us now discuss the various objects of the Page class.
Session
The session object is an instance of the
System.Web.SessionState.HttpSessionState class. It acts as a global repository
to store any type of user specific data that needs to persist between web-page
requests. The session object stores data in the form of name/value pairs which
is used to maintain data like user's name, user's ID, a shopping cart and other
elements which are discarded when a user logs out or is not accessing any page
of the web site. Session sate can be both in process and out process.
Application
The Application object is an instance of the
System.Web.HttpApplicationState class. Like session object, it also stores data
in the form of name/value pairs. However, the scope of this data is global to
the entire application.
Cache
Cache object is an instance of the System.Web.Caching.Cache
class. It also stores global information. It is also a name/value collection of
objects, but we can set customized expiration policies and dependencies for
each item so that items are automatically removed when other resources, such as
files or database tables, are modified.
Request
The request object is an instance of the
System.Web.HttpRequest class. It represents the values and properties of the
HTTP request that caused our page to be loaded. It contains all the URL
parameters and all other information sent by the client.
HttpRequest Properties
·
ApplicationPath and PhysicalPath
ApplicationPath gets the ASP.NET application's virtual
application root path on server while PhysicalPath gets the Physical file
system path corresponding to the requested URL.
·
AnonymousID
This uniquely identifies the current user if we have enabled
anonymous access.
·
Browser
This provides a link to the requesting client's browser
capabilities object.
·
ClientCertificate
This is an HttpClientCertificate object that gets the
security certificate for the current request.
·
Cookies
This gets the collection cookies sent with this request
·
FilePath and CurrentExecutionFilePath
These return the virtual path of the current request.
·
Form
This represents the collection of form variables that were
posted back to the page. In almost all cases we will retrieve the information
from control properties instead of using this collection
·
ServerVariables: This Returns a collection of named server
variables sent with the request.
·
IsAuthenticated: This returns true if the user has been
successfully authenticated.
·
IsSecureConnection: This indicates whether the HTTP connection
uses secure sockets (that is, HTTPS).
·
Islocal: This returns true if the user is requesting the page
from the current system.
·
QueryString: This provides the parameters that are passed along
with the query string.
·
UrlReferrer: This provides a Url object that represents the
current address for the page where the user is coming from (the previous page
that linked to this page).
·
UserAgent: This is a string representing the browser type.
·
UserHostAddress and UserHostName: These get the IP address and
the DNS name of the remote client.
·
UserLanguages: This provides a stored string array of client’s
language preference. This can be useful if we need to create multilingual
pages.
Response
The Response object is an instance of the
System.Web.HttpResponse class, and it represents the web server’s response to a
client request.
HttpResponse Members:
·
Buffer Output
When set to true (the default), the page is not sent to the
client until the entire page is finished processing.
·
Cache
It allows us to configure output caching of a Web page.
·
Cookies
This is the collection of cookies sent with the response.
·
Expires and Expires Absolute
We can use these properties to cache the rendered HTML for
the page for a specified period of time, which helps to improve performance for
subsequent requests.
·
IsClientConnected
This is a Boolean value which indicates whether the client
is still connected to the server.
·
Redirect()
This method transfers the user to another page in the application
or to a different web site.
·
ContentType
The ContentType
is a header that tells the browser what type of content it is about to receive.
In general, ASP.NET web forms use text or html content type. However, we can
create a custom HTTP handler that serves different types of content.
·
OutputStream
It represents the data we are sending to the browser as a
stream of raw bytes.
·
Write()
This method allows us to write text directly to the response
stream.
·
BinaryWrite() and WriteFile()
These methods allow us to take binary content from a byte
array or form a file and write it directly to the response stream.
Server
The Server object is an instance of the
System.Web.HttpServerUtility class. It provides different methods and
properties.
·
MachineName
A property representing the computer name of the computer on
which the page is running, that means the server.
·
CreateObject()
Creates an instance of the COM object that is identified by the
object's programmatic identifier (ProgID).
·
GetLastError
This retrieves the exception object for the most recently
encountered error. This is most commonly used in an application event handler
that checks for error conditions.
Listing 1
Exception LastError;
String ErrMsg;
LastError = Server.GetLastError();
if (LastError != null)
ErrMsg = LastError.Message;
else
ErrMsg = "No Errors";
Response.Write("Last Error = " + ErrMsg);
·
HtmlEncode() and HtmlDecode():
Changes an ordinary string into a string with legal HTML
characters (and back again).
Listing 2
String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);
·
UrlEncode() and UrlDecode():
Changes an ordinary string into a string with legal URL
characters (and back again).
Listing 3
String MyURL;
MyURL = "http://www.technology.com/articles.aspx?title=" +
Server.UrlEncode("ASP.NET Code Snippets");
Response.Write("<a href=" + MyURL + "> ASP.NET Code Snippets </a>");
String DecodedString = Server.UrlDecode(EncodedString);
·
UrlEncodeToken() and URLDecodeToken()
Performs the
same work as UrlEncode() and UrlDecode(), except they work on a byte array that
contains Base64 encoded data.
·
MapPath()
Returns the physical file path that corresponds to a
specified virtual file path on the web server.
Listing 4
String FilePath;
FilePath = Server.MapPath("/MyPortalSite");
·
Transfer()
It can not be used to transfer the user to the site on another
web server or to a non ASP.NET page (such as HTML page or an ASP page). Using
Response.Redirect we can also move to another page. The Response.Redirect
method sends a message to the requesting client to request a new page. This
requires a round trip between the browser and the server, but allows the user
to see the new URL in the browser address bar. Server.Transfer is a quicker
approach in that ASP.NET simply loads the specified page without the round
trip. As a result, the browser’s address bar is not changed.
User
The User object represents a person browsing and using the
applications on a Web site and it allows us to test that user's role
membership. We can authenticate a user based on Windows account information
using IIS or through cookie based authentication with a dedicated login page. If
our web application is performing some sort of authentication that restricts
anonymous users then the user object provides useful information.
Trace
For tracing reader can read my article titled Understanding
Tracing in ASP.NET