Click here to Skip to main content
13,834,471 members
Click here to Skip to main content
Add your own
alternative version


186 bookmarked
Posted 1 Jun 2004
Licenced CPOL

Browser Detection using ASP.NET

, 6 May 2009
Rate this:
Please Sign up or sign in to vote.
Using and updating your machine.config file to better detect browser type in ASP.NET

The old method: Determining browser type in ASP

In web development it is often important to know which browser is viewing the page you are serving. Traditional ASP applications used the Browser Capabilities object that resides in the \system32\inetsrv\browscap.dll DLL. This object takes the User Agent string that a browser sends to a website and compares it to a list of simple wildcard expressions in the browscap.ini file to determine the type of browser currently requesting a page.

The browsercap.ini file has the following form:

[IE 6.0]

[Mozilla/4.0 (compatible; MSIE 6.0*;*Windows NT 5.1*)*]
parent=IE 6.0

If the expression Mozilla/4.0 (compatible; MSIE 6.0*;*Windows NT 5.1*)* matches the user agent string that the browser has sent, then the browser will be identified as IE 6.0 on the WinXP platform, with the properties defined by the [IE 6.0] section. The file is extensible in the sense that you can add your own properties (such as 'crawler', 'stripper' etc) and add your own browser-sniffing filters for newer browsers such as Firebird and Firefox.

To access this information within an ASP page you would do something like the following:

Dim BT
Set BT = Server.CreateObject("MSWC.BrowserType")

Dim BrowserName    : BrowserName    = BT.Browser
Dim BrowserVersion : BrowserVersion = BT.Version

Set BT = nothing

The main problem with this method is that a new version of a new browser needs a new entry. When IE 7 comes out you will have to update your browscaps.ini file. If you don't then you could be rendering your pages incorrectly to your readers or disabling functionality based on incorrect assumptions.

The new method: Determining browser type in ASP.NET

ASP.NET solves this problem by introducing a more powerful regular expression-based method of determining the browser. Instead of relying on simple wildcard searches, the ASP.NET browser detection uses regular expressions to allow more complex testing and data extraction. An example of the format of the browser-sniffing expressions is given below

    <use var="HTTP_USER_AGENT" />
        <!-- Opera -->
        <case match="Opera[ /](?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">

            <filter match="[4-9]" with="${major}">

                <filter match="[5-9]" with="${major}">
            <filter match="^b" with="${letters}">

This branch specifies that the HTTP_USER_AGENT header sent from the browser should be matched against the <filter> expressions that follow. You will notice the expression differs from the traditional ASP method in that it's possible to extract information from the match and assign the extracted values to browser properties. For example, when the user agent string

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2) Opera 7.23 [en]

is matched against the expression

Opera[ /](?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))

the values 'version', 'major' and 'minor' will be set as '7.23', '7' and '.23' respectively.

To access this information you would do something like the following:


Updating browser and platform types

The browser matching regular expressions are contained in the configuration/system.web/browsercaps section of the machine.config file in your \WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG\ directory.

If you wish to add a new platform (eg Windows 2003 or Longhorn) then you can add a new filter under the browsercaps branch below the <use var="HTTP_USER_AGENT" /> directive:

    <case match="Windows NT 5.2|Windows 2003">

To add a new expression to detect Gecko-based browsers you would add

<case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)? (?'type'[^/\d]*)([\d]*)
    <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
        <case match="^b" with="${letters}">

The point here is that future versions of the browser will automatically be detected. As long as your browser-dependant code is of the form if BrowserVersion >= X then ... (and assuming that forward compatibility is maintained for that particular browser) you will be fine.

The Problem, and How You Can Help

With new browsers are constantly being released, and new spiders, crawlers and site-strippers being written it's a difficult job to ensure your config file is up to date. Microsoft has not committed to keeping the machine.config file updated, instead preferring to let cyScape, Inc. do the work. The problem with this is that it isn't being updated, and in fact now seems to have been removed altogether.

Rob Eberhardt at has provided an update for the machine.config file that includes Gecko, Safari and Konqueror browsers, and I've made a small update to demonstrate how to include detection for Windows 2003. There is an excellent browsercap.ini file maintained by Gary Keith at that lists not only browsers, but all crawlers, spiders and strippers that have been detected. Gary has done an outstanding job.

Rob Eberhardt and I would like to use the combined resources of the CodeProject community, along with the amassed information provided by Gary to start our own CodeProject maintained browsercaps compilation for ASP.NET machine.config files. With your help we can ensure that our browser detection is up to date.

A test page has been provided here. If you have a browser that is mis-reported or not detected then please either let us know or better yet, take the time to determine the expression matches needed and send them in so we can merge them. Better yet, if someone has the time and patience to develop a database driven config file generator then that would automate the process and speed up updates.

For the moment we will do our best to ensure the current file is updated as often as possible. It's over to you.



  • 2 Jun 2004 - first posted
  • 9 Dec 2004 - Rob Eberhardt updated the XML file to pick up plain (non-Firefox) Mozilla correctly, missed some Safari and Konqueror variations, and added an Opera section
  • 4 Jul 2005 - Updated XML file, NUnit test library and simple command line tool to test useragents provided by Owen Brady
  • 10 Oct 2005 - Updated XML file with updates for Opera/Gecko/AppleWebKit based browsers. Mainly to properly detect version and other settings according to there websites information. Update provided by Owen Brady
  • 18 May 2006 - Updated XML file using JWhite's suggestions.
  • 25 July 2006 - More updates from Owen Wilson for Opera/Gecko/AppleWebKit browsers
  • 6 May 2009 - updated the resource section


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...


Comments and Discussions

Generali am using same browserCaps.config file in my nopcommerce Pin
Dodda Dorababu26-Jul-12 2:39
memberDodda Dorababu26-Jul-12 2:39 
NewsThis article is outdated Pin
Member 74095415-Sep-10 21:19
memberMember 74095415-Sep-10 21:19 
GeneralDetect Browser pop up blocker settings in client machine Pin
PujaJayaraj8-Dec-09 18:11
memberPujaJayaraj8-Dec-09 18:11 
GeneralRecognize Chrome browser in ASP.NET Pin
S.Makas1-Sep-09 4:06
memberS.Makas1-Sep-09 4:06 
GeneralRe: Recognize Chrome browser in ASP.NET Pin
Joshua Blackstone2-Dec-09 7:41
memberJoshua Blackstone2-Dec-09 7:41 
GeneralRe: Recognize Chrome browser in ASP.NET Pin
brench28-Feb-10 10:09
memberbrench28-Feb-10 10:09 
QuestionBrowser Detection using JSP/PHP? Pin
adatapost6-May-09 17:32
groupadatapost6-May-09 17:32 
Questionhow do we detect IIS is running thru code? Pin
Deep_blue200712-Dec-07 23:56
memberDeep_blue200712-Dec-07 23:56 
GeneralOs Vista & XP Pin
Tal Hakemon12-Mar-07 21:30
memberTal Hakemon12-Mar-07 21:30 
QuestionNot detecting the W3C Validator!!! Pin
Adam Tibi22-Sep-06 0:46
professionalAdam Tibi22-Sep-06 0:46 
AnswerRe: Not detecting the W3C Validator!!! [modified] Pin
Jorge Galindez18-Nov-06 6:40
memberJorge Galindez18-Nov-06 6:40 
GeneralRe: Not detecting the W3C Validator!!! Pin
Adam Tibi19-Nov-06 23:31
professionalAdam Tibi19-Nov-06 23:31 
GeneralFirefox Pin
j1mb0jay10-Aug-06 6:00
memberj1mb0jay10-Aug-06 6:00 
QuestionsupportsFileUpload Pin
Alexandru Stanciu20-Jul-06 0:15
memberAlexandru Stanciu20-Jul-06 0:15 
AnswerRe: supportsFileUpload Pin
ocean10000025-Jul-06 20:48
memberocean10000025-Jul-06 20:48 
QuestionMobileCapabilities section &lt;browserCaps&gt; in machine.config Pin
yahome19-Jun-06 23:53
memberyahome19-Jun-06 23:53 
AnswerRe: MobileCapabilities section &lt;browserCaps&gt; in machine.config Pin
Amimpat10-Aug-09 22:50
memberAmimpat10-Aug-09 22:50 
GeneralDetect crawlers and spiders Pin
Gustav Evertsson3-May-06 10:37
memberGustav Evertsson3-May-06 10:37 
GeneralTest page error, Opera Browser Pin
JewleBewle25-Apr-06 0:34
memberJewleBewle25-Apr-06 0:34 
GeneralWinXP Detection Pin
ray4826-Apr-06 4:54
memberray4826-Apr-06 4:54 
GeneralRe: WinXP Detection Pin
ray4826-Apr-06 7:34
memberray4826-Apr-06 7:34 
GeneralQuestion on changing browercaps in machine.config Pin
smh116-Mar-06 9:55
membersmh116-Mar-06 9:55 
QuestionGetting the OS Pin
Justincc24-Feb-06 9:42
professionalJustincc24-Feb-06 9:42 
QuestionBrowser Name Detection Pin
Adam Tibi21-Feb-06 2:23
professionalAdam Tibi21-Feb-06 2:23 
AnswerRe: Browser Name Detection Pin
Chris Maunder21-Feb-06 4:43
adminChris Maunder21-Feb-06 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190114.1 | Last Updated 6 May 2009
Article Copyright 2004 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid