<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Creating a Custom Browser Setup with ASP.NET 2.0
By John Mueller

Rate This Article: Add This Article To:

Creating a Custom Browser Setup with ASP.NET 2.0 - ' The Painful Old Way '
( Page 2 of 3 )

ASP.NET 2.0 has a wealth of new features to offer: everything from an improved security model to new controls. However, the most interesting new feature for many developers may not ASP.NET's improved widgets for developing applications, but the improved control that custom browser setups can provide. Instead of waiting for Microsoft to add support for a particular browser, you can add this functionality yourself using a specially formatted .BROWSER file.

The old method of detecting a browser relies on BrowserCap.DLL. This DLL detects the user agent entry, provided by the client machine, by using a simple wildcard match. It then looks in the BrowserCap.INI file to locate information about that browser. Listing 1 shows a typical example of a BrowserCap.INI file entry.

Listing 1. A Typical BrowserCap.INI Browser Entry.

[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

There isn't anything too mysterious about this file. Its settings define whether the browser includes support for specific features. In fact, you can extend BrowserCap.INI to include custom entries; or you can add entries for other browsers, by simply creating a new key, like the [IE 6.0] entry shown.

The problem with this technique is twofold. First, the setup relies on a wildcard search, which means that more than one browser could match the search criteria. The system always uses the first matching browser, so some clients might receive the wrong information based on an incorrect set of capabilities. Second, using this technique means that you must add a new entry for every new version of a browser. When Microsoft introduces Internet Explorer 7.0, you'll need to add an [IE 7.0] key.

Developing a New Approach in ASP.NET

Microsoft sought to make things easier when it introduced ASP.NET. Hidden deep within the Machine.CONFIG file (found in the \WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG folder) is a special section called <browserCaps>. This section contains a series of XML entries that define the configurations for various browsers based on a regular expression. This entry is relatively long, but its most interesting feature is a series of case statements. Listing 2 shows an example of these entries (very much shortened for this article).

Listing 2. A Typical Machine.CONFIG Browser Entry

<browserCaps>
   <use var="HTTP_USER_AGENT"/>
      <case match="WebTV/
      (?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
      browser=WebTV
      version=${version}
      majorversion=${major}
      minorversion=${minor}
      tables=true
      cookies=true
      backgroundsounds=true
      isMobileDevice="true"
      <filter match="2" with="${minor}">
         javascript=true
         ecmascriptversion=1.0
         css1=true
      </filter>

These entries are an improvement, because they provide several features that the INI file could not. Notice that the version number of a browser need not impair the functionality that your application can provide. The system assigns the information it finds in the HTTP_USER_AGENT header entry — supplied by the client — to the browser capabilities output. In addition, you can add filters to assign specific information based on version.

Using this information is also relatively easy in ASP.NET. All you need to do is obtain access to the Browser object supplied as part of the request. The following code displays the browser name and version number.

Response.Write(Request.Browser.Browser.ToString());
Response.Write(Request.Browser.Version.ToString());

The only problem with the Machine.CONFIG setup is that few people know that it exists. In addition, Microsoft doesn't make updates to these entries. If you look earlier in the file, Microsoft tells you to obtain updates for this file from cyScape, Inc. as shown here.

<!-- For updates to this browser data visit cyScape, Inc. at
http://www.cyscape.com/browsercaps -->

Unfortunately, cyScape is very slow about providing updates for new browsers, which means that it's likely that you won't find the update you need. Fortunately, a couple of other .NET developers are making the process a little easier. You can find updates for the Gecko, Safari, and Konqueror browsers on Rob Eberhardt's Web site. An update that permits detection of the Windows 2003 platform appears on Gary Keith's Web site.



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By John Mueller