Phil Scott's WebLog

Quite exciting this computer magic

Watch Out for Request.Browser.MajorVersion in ASP.NET 2.0

I awoke to 1500 exceptions published to me this morning from our public site running ASP.NET 2.0.  Not good times.  "Luckily" they were all the same: Value cannot be null.  It seems one of the components that we are using is calling Request.Browser.MajorVersion and blowing up on goofy user-agents.  In this case the user-agent of doom is "YahooSeeker/1.2 (compatible; Mozilla 4.0; MSIE 5.5; yahooseeker at yahoo-inc dot com ; http://help.yahoo.com/help/us/shop/merchant/)".  For whatever reason ASP.NET 2.0 cannot parse the MajorVersion out of that and tries to call int.Parse on a null value, causing an error.

Luckily we own the source code to this particular component, so I just wrapped the calls to MajorVersion in a try block so that the entire site doesn't blow up on bogus error codes, so hopefully if you run into that particular error you'll be able to track it down.  I haven't tried any of the other properties of the Browser class, but if you are relying on them for anything you might want to first test them out with a rediculous user agent and see what blows up. 

Comments

Dylan said:

Or even better, don't think in terms of browser versions when printing out client side scripts etc. Either write using standards level code and forget anyone else, or do tests such as

if (document.getElementById)
{
//standard browser
} else if (document.all)
{
//IE junk
}

Now, I'm not saying this applies to your particular issue that you are dealing with or that you don't already know this approach. I just wanted to emphasize to other readers that supplying different (eg. down-level) content depending on a browser version is a bad idea.

There are uses for checking browser version though, such as serving up different content for search engine bots etc.

# December 7, 2005 2:26 PM

Phil Scott said:

I believe this particular component was trying to figure out what "features" it was going to provide. For example, with IE it will use the DirectX filters to do some fancy animations.

Personally, the closest I'll come to browser specific code will be using CSS hacks to work around IE's flaws.
# December 7, 2005 3:13 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)