Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, August 30, 2006

Browser Detection using 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

                                   browser=Opera            version=${version}            majorversion=${major}            minorversion=${minor}            frames=true                tables=true            cookies=true            javascript=true            ecmascriptversion=1.1            isMobileDevice="true"                            ecmascriptversion=1.3                css1=true                css2=true                xml=true                                    w3cdomversion=1.0                                                            beta=true           

This branch specifies that the HTTP_USER_AGENT header sent from the browser should be matched against the 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:

Continue with Updated Article

, <% response.write(request.browser.browser.tostring()); response.write(request.browser.version.tostring());%>

Post a Comment