|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
The old method: Determining browser type in ASPIn 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]
browser=IE
version=6
majorver=6
minorver=0
css=2
frames=True
iframes=True
tables=True
cookies=True
backgroundsounds=True
vbscript=True
javascript=True
javaapplets=True
activexcontrols=True
cdf=True
aol=False
beta=False
win16=False
crawler=False
stripper=False
wap=False
netclr=False
AK=False
SK=False
[Mozilla/4.0 (compatible; MSIE 6.0*;*Windows NT 5.1*)*]
parent=IE 6.0
platform=WinXP
If the expression 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.NETASP.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 <browserCaps>
<use var="HTTP_USER_AGENT" />
<filter>
<!-- Opera -->
<case match="Opera[ /](?'version'(?'major'\d+)(?'minor'\.\d+)
(?'letters'\w*))">
browser=Opera
version=${version}
majorversion=${major}
minorversion=${minor}
frames=true
tables=true
cookies=true
javascript=true
ecmascriptversion=1.1
isMobileDevice="true"
<filter match="[4-9]" with="${major}">
ecmascriptversion=1.3
css1=true
css2=true
xml=true
<filter match="[5-9]" with="${major}">
w3cdomversion=1.0
</filter>
</filter>
<filter match="^b" with="${letters}">
beta=true
</filter>
</case>
This branch specifies that the 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: <%
Response.Write(Request.Browser.Browser.ToString());
Response.Write(Request.Browser.Version.ToString());
%>
Updating browser and platform typesThe browser matching regular expressions are contained in the If you wish to add a new platform (eg Windows 2003 or Longhorn) then you can add a new filter under the <filter>
<case match="Windows NT 5.2|Windows 2003">
platform=Win2003
</case>
</filter>
To add a new expression to detect Gecko-based browsers you would add <case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)? (?'type'[^/\d]*)([\d]*)
/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)).*">
browser=Gecko
type=${type}
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="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
version=${version}
majorversion=${major}
minorversion=${minor}
<case match="^b" with="${letters}">
beta=true
</case>
</case>
</case>
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 The Problem, and How You Can HelpWith 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. at http://www.cyscape.com/browsercaps do the work. The problem with this is that it isn't being updated and made available to the public in an easily downloadable and consumable form. Rob Eberhardt at http://slingfive.com/demos/browserCaps 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 http://www.garykeith.com/browsers/ 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. History
|
||||||||||||||||||||||