ASP.NET Basics (Part 7): Command and Control - Requesting More
(Page 3 of 7 )
Before moving on to more complex form processing, let's dawdle a little bit with our new acquaintance, the Request object, and find out a little more about it. The Request object is one of the more useful objects you'll encounter in your ASP.NET development, since the concept of a request and a response is the backbone of the client-server architecture. The Internet is, after all, a vast network of servers, with each of us accessing it through clients. For each bit of information that we wish to access, there is a "request" sent to some server that in turn sends the appropriate "response" back to the browser (client).
In addition to the "QueryString" and "Form" properties you just saw, the Request object also comes with a bunch of other useful properties; the following example demonstrates some of them:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<td><b>Variable</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>Hostname</td>
<td>
<% Response.Write (Request.UserHostName) %>
</td>
</tr>
<tr>
<td>Host address</td>
<td>
<% Response.Write (Request.UserHostAddress) %>
</td>
</tr>
<tr>
<td>URL</td>
<td>
<% Response.Write (Request.Url) %>
</td>
</tr>
<tr>
<td>Path to script</td>
<td>
<% Response.Write (Request.PhysicalPath) %>
</td>
</tr>
<tr>
<td>Browser</td>
<td>
<% Response.Write (Request.UserAgent) %>
</td>
</tr>
</table>
</body>
</html>
And when you view the file in your browser, you'll probably see something like this:

Why stop there? ASP.NET also provides a Browser object (a property of the Request object that, coincidentally, is itself an object) that provides information about the HTTP client you're using. Here is an example of how it can be used:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<td><b>Variable</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>Client version</td>
<td>
<% Response.Write (Request.Browser.Version) %>
</td>
</tr>
<tr>
<td>Supports tables?</td>
<td>
<% Response.Write (Request.Browser.Tables) %>
</td>
</tr>
<tr>
<td>Supports frames?</td>
<td>
<% Response.Write (Request.Browser.Frames) %>
</td>
</tr>
<tr>
<td>Supports Java applets?</td>
<td>
<% Response.Write (Request.Browser.JavaApplets) %>
</td>
</tr>
<tr>
<td> Supports JavaScript?</td>
<td>
<% Response.Write (Request.Browser.JavaScript) %>
</td>
</tr>
<tr>
<td>JavaScript version supported</td>
<td>
<% Response.Write (Request.Browser.EcmaScriptVersion) %>
</td>
</tr>
</table>
</body>
</html>
Here's the output:

All these variables come in handy if you need to make decisions on the basis of remote variables, as the following example demonstrates:
<script language="c#" runat="server">
void Page_Load()
{
string browser = Request.UserAgent;
if(browser.IndexOf("MSIE") >= 0)
{
// IE-specific code
output.Text = "You're using Internet Explorer, aren't you?";
}
else if(browser.IndexOf("Mozilla") >= 0)
{
// Mozilla-specific code
output.Text = "Mozilla rocks, dood! Gimme five!";
}
else
{
// any other browser
output.Text = "Hmmm, you're using a browser I don't know about. Never mind, you're welcome too!";
}
}
</script>
<html>
<head></head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>
Note my usage of the IndexOf() method of the "string" object (the "string" datatype, like just about everything else in the .NET environment, is an object, with its own set of properties and methods) - this method is used to scan a string for a specific character sequence. It returns 0 if the sequence cannot be matched, and is thus very useful to run browser detection routines, as in the sample above.
Next: Test Drive >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire