Overview
In this article we are going to learn about the HttpBrowserCapabilities component of
ASP.NET which will allow you to detect the user browser and develop browser friendly
pages.
HttpBrowserCapabilites Component
HttpBrowserCapabilities is a class present in the System.Web namespace. To use it
you don't have to create new instance of this class, simply use the one provided by
Page.Request.Browser property within the ASP.NET page.
To see the complete listing of constructors and properties of HttpBrowserCapabilities
class, use the following link :
Coding the ASP.NET page
To demonstrate the different properties of HttpBrowserCapabilities class, we'll
create an ASP.NET page. Create a new .aspx page and save it as browserCaps.aspx in
any directory from where you can run ASP.NET pages. Now copy and paste the following
code in it :
<%@ Page Language="C#" %>
<html>
<head>
<style>
p { font-family:Tahoma, Sans-Serif; font-size:10pt; }
td { font-family:Tahoma, Sans-Serif; font-size:8pt; padding-left:10; }
tr { background-color:#F7F7F7; }
.title { color:#396B9C; font-weight:bold; }
.header { background-color:#CCDDEE; }
</style>
</head>
<body>
<!-- p class="title">Browser Capabilities :</p -->
<table width="90%" border="0" align="center"
cellpadding="2" cellspacing="2">
<tr class="header">
<td width="30%">Property</td>
<td>Value</td>
</tr>
<tr>
<td>ActiveXControls </td>
<td> <%= Request.Browser.ActiveXControls %></td>
</tr>
<tr>
<td>AOL </td>
<td> <%= Request.Browser.AOL %></td>
</tr>
<tr>
<td>BackgroundSounds </td>
<td> <%= Request.Browser.BackgroundSounds %></td>
</tr>
<tr>
<td>Beta </td>
<td> <%= Request.Browser.Beta %></td>
</tr>
<tr>
<td>Browser </td>
<td> <%= Request.Browser.Browser %></td>
</tr>
<tr>
<td>CDF </td>
<td> <%= Request.Browser.CDF %></td>
</tr>
<tr>
<td>ClrVersion </td>
<td> <%= Request.Browser.ClrVersion %></td>
</tr>
<tr>
<td>Cookies </td>
<td> <%= Request.Browser.Cookies %></td>
</tr>
<tr>
<td>Crawler </td>
<td> <%= Request.Browser.Crawler %></td>
</tr>
<tr>
<td>EcmaScriptVersion </td>
<td> <%= Request.Browser.EcmaScriptVersion %></td>
</tr>
<tr>
<td>Frames </td>
<td> <%= Request.Browser.Frames %></td>
</tr>
<tr>
<td>JavaApplets </td>
<td> <%= Request.Browser.JavaApplets %></td>
</tr>
<tr>
<td>JavaScript </td>
<td> <%= Request.Browser.JavaScript %></td>
</tr>
<tr>
<td>MajorVersion </td>
<td> <%= Request.Browser.MajorVersion %></td>
</tr>
<tr>
<td>MinorVersion </td>
<td> <%= Request.Browser.MinorVersion %></td>
</tr>
<tr>
<td>MSDomVersion </td>
<td> <%= Request.Browser.MSDomVersion %></td>
</tr>
<tr>
<td>Platform </td>
<td> <%= Request.Browser.Platform %></td>
</tr>
<tr>
<td>Tables </td>
<td> <%= Request.Browser.Tables %></td>
</tr>
<tr>
<td>Type </td>
<td> <%= Request.Browser.Type %></td>
</tr>
<tr>
<td>VBScript </td>
<td> <%= Request.Browser.VBScript %></td>
</tr>
<tr>
<td>Version </td>
<td> <%= Request.Browser.Version %></td>
</tr>
<tr>
<td>W3CDomVersion </td>
<td> <%= Request.Browser.W3CDomVersion %></td>
</tr>
<tr>
<td>Win16 </td>
<td> <%= Request.Browser.Win16 %></td>
</tr>
<tr>
<td>Win32 </td>
<td> <%= Request.Browser.Win32 %></td>
</tr>
<%
b = null;
%>
</table>
</body>
</html>
browserCaps.aspx Code Explanation
The first line tells that the language we are going to use on our ASP.NET page is C#.
<%@ Page Language="C#" %>
Next we display the values of all the properties of HttpBrowserCapabilites class
by simply writing them to the user scren using Response.Write() method. The complete
path to get a property of HttpBrowserCapabilites class is actually Page.Request.Browser.PropertyName
where PropertyName can be any of the properties of HttpBrowserCapabilites class. But
we can use a shortcut like the one we used in our browserCaps.aspx page i.e.
Request.Browser.PropertyName, because when using Request object, it is assumed
Page.Request.
Developing Browser Friendly Pages
Property names of HttpBrowserCapabilites class are quite suggestive of what they
tell. You can for example use Crawler property to determine if the client is a
search engine crawler or not, and if it is then show some more search engine
specifc content.
If your site uses JavaScript, CSS, DHTML etc, you can get info about the browser
if it supports these features or not by looking at JavaScript, Browser and Version
properties. Internet Explorer and Netscape browsers are represented as "IE" and
"Netscape" in the Browser property respectively. Version 4.0 and above of these
browsers support many of the features listed above, but Netscape 6 is far better than
Netscape 4.x when it comes to rendering CSS rich web pages. You can determine if the
client is using Netscape 6 by using a C# code like following :
if(Request.Browser.Browser.Equals("Netscape") &&
Request.Browser.MajorVersion >= 6)
Response.Write("Thank God! you are using a better version of Netscape.");
Above code will display a "Thanks God!" message to Netscape 6 users only. MajorVersion
property returns a 32 bit Integer ( Int32 ) so we can easily use ">" and "=" operators
to check for the correct version.
Following code displays "Yours is the best!" message to Internet Explorer 5.x and above
browsers :
if(Request.Browser.Browser.Equals("IE") && Request.Browser.MajorVersion >= 5)
Response.Write("Yours is the best!");
Hope you've understood how to display different content to different browsers by
looking at the properties of HttpBrowserCapabilites class. Now run the browserCaps.aspx
page in your browser and notice the different values you get. Following image shows
how browserCaps.aspx appeared in my Internet Explorer 5.5 browser :

browserCaps.aspx