ASP.Net Browser Detection

Posted: 14/08/2006 18:12:31
Author: Andy Pilgrim
Is ASP.Net 1.x Compatible? Yes! ASP.Net 1.x Compatible (?)

    Live Demonstration

    Both ASP.Net 1.1 and 2.0 provide methods for determining which browser is requesting a page. Though the internal workings differ between ASP.Net 1.1 and 2.0 (more on this later), the techniques for retrieving browser information are identical. As well as identifying which browser is in use, ASP.Net can also return information about the capabilities of the browser.

    Browser information is retrieved from the HttpBrowserCapabilities class; an instance of this class is initialised for each page request and can be accessed from Request.Browser. Some of the most useful properties of this class are described in the table below.

    Browser String. Gets the name of the browser
    MajorVersion Integer. Gets the major version of the browser (e.g. 6)
    MinorVersion Decimal. Gets the minor version of the browser (e.g. 0)
    Platform String. Gets a the name of the platform the browser is running on (e.g. WinXP)

    Displaying browser information in a webpage is straight forward. In the code listing below, I've used <%= %> syntax for clarity, although you may prefer a collection of label server controls.

    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Browser Detection</title>
    </head>
    <body>
        <table border="1">
            <tr><td colspan="2">Raw Content</td></tr>
            <tr>
                <td>User Agent String</td>
                <td><%=Request.UserAgent%></td>
            </tr>
            <tr><td colspan="2">Browser Detection Properties</td></tr>
            <tr>
                <td>Platform</td>
                <td><%=Request.Browser.Platform%></td>
            </tr>
            <tr>
                <td>Browser</td>
                <td><%=Request.Browser.Browser%></td>
            </tr>
            <tr>
                <td>Major Version</td>
                <td><%=Request.Browser.MajorVersion%></td>
            </tr>
            <tr>
                <td>Minor Version</td>
                <td><%=Request.Browser.MinorVersion%></td>
            </tr>
            <tr>
                <td>Crawler</td>
                <td><%=Request.Browser.Crawler%></td>
            </tr>
        </table>
    </body>
    </html>

    This code outputs a table containing browser properties to the webpage. A live demonstration of the code can be viewed here.

    Why Bother?

    You may say that's all well and good, but what use is it to me?

    Internally, ASP.Net uses browser detection to determine which HTML and JavaScript to output to the client. For example, client-side validation can be employed on browsers which support JavaScript. However, this behaviour is for the most part behind-the-scenes and no intervention is required.

    Browser usage, however, is of great interest to webmasters, particularly when coupled with traffic statistics. Webmasters may infer a great deal from which platform and browser their visitor use. However of greater interest is the ability to detect crawlers, their behaviour can be of use in search engine optimisation. For example, I can tell which search engines list my site by checking whether their crawlers have visited my pages.

    Browser detection can also allow pages to output different mark-up based on which browser is in use. For-example, IE users may be treated to an ActiveX control whereas non-IE users may get a plain HTML form. This approach is generally not recommended due to the propensity of browser detection to be erroneous. It is this we cover next.

    Accuracy

    Browser detection is a notoriously difficult game. In ASP.Net the browser is detected based on the user agent (UA) string sent with the request. There exists no standards for UA strings, and hence the reliability of browser detection depends on the ability of ASP.Net to interpret the UA string. Some browsers also allow users to define custom UA strings, allowing users to trick ASP.Net browser detection. In preparation for this article, I made a simple script to log visits to this site and key properties of Request.Browser. Initially I was impressed with the results: IE, Firefox, Mozilla and Opera seemed to be accurately reflected, as well as many common platforms (WinXP, Win2000 and Linux). However Windows Vista was interpreted as WinNT, and Konqueror was reported as Mozilla. Also, detection of crawlers was disappointing "Yahoo! Slurp" and "Googlebot" were both reported as Mozilla, the former not even flagged as a crawler. How can this be rectified?

    This depends on which version of ASP.Net you are using:

    • ASP.Net 1.1 allows you to define browser detection rules in the web.config file. The syntax for this is covered in the next section.
    • In ASP.Net 2.0, the above method is depreciated in favour of .browser files in the App_Browsers folder. The old technique, however, does still work. You can read more about .browser files at MSDN

    Unfortunately, no official source for these updates exists. Rob Eberhardt has created a good set of updates at http://slingfive.com/pages/code/browsercaps/ and there are some more from this Code Project article, download the .zip file (registration required). These are updates for the legacy web.config definitions. You can write your own ASP.NET 1.1 style definitions from this reference.

    Updates to the web.config File

    In this section I will demonstrate how to extend the default detection abilities using the web.config approach (i.e. this is compatible with all versions of .Net). To begin, suppose we want to add detection for Googlebot which has a typical UA string Mozilla/5.0 (compatible; Googlebot/2.1; http://www.google.com/bot.html). Thus we add the following to system.web in the web.config file:

    <browserCaps>
     <use var="HTTP_USER_AGENT" />
     <filter>
      <!-- Googlebot -->
      <case match="Googlebot/(?’‘major’‘\d+)(?’‘minor’‘\.\d+)">
        browser=Googlebot
        crawler=true
        majorversion=${major}
        minorversion=${minor}
      </case>
     </filter>
    </browserCaps>

    The <use var="HTTP_USER_AGENT" /> tag tells ASP.Net to detect using the UA string. Then <case> defines a regular expression to match, in this case for the Googlebot UA string. Here we capture the version and set the properties browser, crawler, majorversion and minorversion inside the <case> tag. These correspond directly to the output values of Request.Browser.*. We could set values for the other properties if we wanted, but this is omitted for breviety.

    Matching the platform is along similar lines. Windows Vista identifies itself as Windows NT 6.0 hence the following addition to web.config adds Windows Vista identification.

    <filter>
     <case match="Windows NT 6.0">
      platform=WinVista
     </case>
    </filter>

    Regular expressions presented here are fairly straight forward, however, they can be as complex as you like! I suggest looking at the Code Page article for some good sample as well as a great update for your web.config files.

    Where to Now?

    My original motivation for looking into browser detection with ASP.Net was to build a website traffic analysis tool. With some modifications, ASP.Net’‘s built-in detection may be useful in categorising requests. I'll cover an ASP.Net implementation of traffic analysis in a future article, so stay tuned!