Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Detect the browser using ASP.NET and C#

0.00/5 (No votes)
19 Dec 2001 1  
A Simple ASP.NET program for detecting browser type that clients use

This article intends to show how to detect the browser on the clients side. I use the HttpBrowserCapabilities class using the C# language. The HttpBrowserCapabilities class gives information on the capabilities of the browser that is running on the client. It needs the  System.Web namespace. HttpBrowserCapabilities properties are accessible through the Browser property of ASP.NET's intrinsic Request object. Here's the HttpBrowserCapabilities members:

 Properties  Meaning
 ActiveXControls Gets a value indicating whether the client browser supports ActiveX controls.
 AOL Gets a value indicating whether the client is an America Online (AOL) browser.
 BackgroundSounds Gets a value indicating whether the client browser supports background sounds.
 Beta Gets a value indicating whether the browser is a beta release.
 Browser Gets the browser string (if any) that was transmitted in the User-Agent header.
 CDF Gets a value indicating whether the client browser supports Channel Definition Format (CDF) for webcasting.
 ClrVersion [To be supplied.]
 Cookies Gets a value indicating whether the client browser supports cookies.
 Crawler Gets a value indicating whether the client browser is a Web crawler search engine.
 EcmaScriptVersion Gets the version number of ECMA script that the client browser supports.
 Frames Gets a value indicating whether the client browser supports HTML frames.
 Item (inherited from HttpCapabilitiesBase) Allows access to individual dictionary values.

In C#, this property is the indexer for the HttpBrowserCapabilities class.

 JavaApplets Gets a value indicating whether the client browser supports Java applets.
 JavaScript Gets a value indicating whether the client browser supports JavaScript.
 MajorVersion Gets the major (that is, integer) version number of the client browser.
 MinorVersion Gets the minor (that is, decimal) version number of the client browser.
 MSDomVersion Gets the version of Microsoft XML Document Object Model (DOM) that the client browser supports.
 Platform Gets the name of the platform that the client uses.
 Tables Gets a value indicating whether the client browser supports HTML tables.
 Type Gets the name and major (that is, integer) version number of the client browser.
 VBScript Gets a value indicating whether the client browser supports VBScript.
 Version Gets the full (integer and decimal) version number of the client browser.
 W3CDomVersion Gets the version of the World Wide Web Consortium (W3C) XML Document Object Model (DOM) that the client browser supports.
 Win16 Gets a value indicating whether the client is a Win16-based machine.
 Win32 Gets a value indicating whether the client is a Win32-based machine.

For more information about HttpBrowserCapabilities class, click here

This code lines for ASP.NET using C# language:

 <%@ Page Language="C#" %>
<html>

 <head>
 <title>Browser Information</title>
 </head>

 <body>

 <p><font face="Arial" size="3"><b>Browser Information</b></font></p>
 <hr align="left" width="30%" color="#04ACB5">
 <div align="left">
  <table border="0" width="44%">
    <tr>
      <td width="34%"><font face="Verdana" size="2">Browser Type</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Browser %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Version</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Version %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">ActiveX Control</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.ActiveXControls %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Java Script</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.JavaScript %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Java Applet</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.JavaApplets %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">VB Script</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.VBScript %></font></td>
    </tr>
  </table>
 </div>
 <hr align="left" width="30%" color="#04ACB5">
 <p><font face="Times New Roman" size="2" color="#000000">Copyright &copy 2001 <a href="mailto:agusasia@yahoo.com"> Agus Kurniawan</a></font></p>

 </body>

 </html>

Save this file as detectbrowser.aspx and put on your IIS directory. Run it !!.

Reference

Microsoft Studio.NET Documentation

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here