The Browser Detection Problem in ASP.NET
March 27th, 2007 — 10:37 pmOne of the advantages of ASP.NET is its ability to deal with the incompatibilities that exist between browsers. Because ASP.NET is a server side technology that generates web pages dynamically, it’s able to change the HTML it renders at runtime. So if I place a menu control on my page ASP.NET will render the HTML code that is specific to the browser that’s requesting the page. This is great since it frees the developer from having to deal with all the browser incompatibilities that has become the bane of most web developers.
With the release of .Net 2.0 all of the server controls are cross browser compatible, according to Microsoft, so you would think that dealing with browser incompatibilities would be a thing of the past for the ASP.NET developer. Unfortunately this is not the case. While it is true that the server controls that ship with .Net 2.0 are cross browser compatible, it doesn’t mean they will always work in the various browsers as expected. Sounds like a contradiction doesn’t it? Although .Net is quite good at rendering the appropriate HTML depending on the browser and its various capabilities, the real problem is actually determining the user’s browser and its capabilities.
The .Net framework contains a list of browser capabilities, often referred to as Browser CAPS, that’s used to help determine the browser and its capabilities when a page is requested. From this information the proper HTML is rendered for the different ASP.NET server controls. There are a lot of browsers and a lot of different versions with a lot of different capabilities so as you can imagine the information list is large and ever changing. Unfortunately, a few years back Microsoft decided to outsource the maintenance of this list to a company called Cyscape (www.cyscape.com) who has done an abysmal job at best. This shouldn’t come as a surprise since Cyscape has been hawking its own product, BrowserHawk, to solve this very same problem. If that’s not a conflict of interest I don’t know what is.
This Browser CAPS information list is not proprietary and is open and modifiable by anyone so in theory you don’t need to wait for Cyscape to make the updates and corrections. The Browser CAPS info for a .Net 2.0 installation is located (on my machine) in a folder:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers
In this folder you’ll find a list of xml files with “.browser” file extensions that contain all the information on the given browser family and its capabilities. The information here is used by all .Net web applications running on your system. If you have access to this folder you can edit these files yourself rather than wait for Cyscape to do it and have it come through as a .Net update.
If you don’t have access to this folder, for instance if you’re having your website hosted, you can also add the information to the web.config file of your website. The custom browser capability info should be placed in the system.web section of the web.config file as shown here:
< system.web >
< browserCaps >
< case match="AppleWebKit/(?’version‘(?’major’\d?)(?’minor’\d{2})(?’letters’\w*)?)" >
browser= AppleWebKit
version=${version}
majorversion=0${major}
minorversion=0.${minor}
frames=true
tables=true
cookies=true
javascript=true
javaapplets=true
ecmascriptversion=1.5
w3cdomversion=1.0
css1=true
css2=true
xml=true
tagwriter=System.Web.UI.HtmlTextWriter
< case match=" AppleWebKit/(?’version’(?’major’\d)(?’minor’\d+)(?’letters’\w*))(.* )?(?’type’[^/\d]*)/.*( |$)" >
type=${type}
< /case >
< /case >
< /browserCaps >
< /system.web >
Although this approach allows you to specifiy the browser detection and capabilities information on systems where you have restricted access, it does mean the information is not global and must be specified in the web.config file for each website on the webserver. For most people this isn’t much more than an inconvenience that’s easily overshadowed by the effort saved when ASP.NET renders the correct HTML for you.
So that leaves us with one final question, how do we know what to set for the various parameters? Unfortunately this isn’t an easy question to answer. If you’re having problems with the rendering for a particular browser the first place to look is the browser’s website. Many browser vendors have Browser CAPS settings in their support sections or can at least point you to a resource with the correct settings.
Rob Eberhardt also maintains a web page with some useful information on browser caps:
http://slingfive.com/pages/code/browserCaps/
For a more detailed treatment of how ASP.NET manages browser detection, including how you can define your own browser settings you might want to check out the following article on MSDN:
http://msdn.microsoft.com/msdnmag/issues/05/01/ASPColumn/
It’s unfortunate that Microsoft has outsourced this very important piece of work to a company with an obvious conflict of interest. This move has decreased the productivity gain from using a supposedly cross browser capable web development platform as well as increased the frustration of many .Net developers. Having said that, and I’d hate to support Cyscape for their lack of support on this issue, but if you really need to solve the browser detection problem and trying to keep up with all the changes is too time consuming and costly, then BrowserHawk may be the answer to your problems.
