Browser

Manipulating ASP.NET 2.0 browser support

ASP.NET 2.0 allows you to address browser support through browser configuration files. This set of files defines the capabilities for specific browsers. In addition, you can include browser files within a project to override settings for specific browsers.

Even though we are at version 3.5 of the .NET Framework and ASP.NET, I am still having fun exploring some of the cool features that were added in version 2.0.

For instance, ASP.NET 2.0 allows you to address browser support through browser configuration files. This set of files defines the capabilities for specific browsers. In addition, you can include browser files within a project to override settings for specific browsers.

Default browser files

An ASP.NET installation includes a set of browser files. The files use the .browser file extension and use XML to define the various browser characteristics. The files are named according to the browser they define; for instance, nokia.browser accommodates the browser used by Nokia devices. The browser files are located in the following directory:

<root Windows folder>Microsoft.NETFramework<version>CONFIGBrowsers

For example, the files are located in the following directory on a Windows XP machine using .NET Framework 2.0:

WinntMicrosoft.NETFrameworkv2.0.50727CONFIGBrowsers

This directory includes a number of browser files that define the capabilities for specific browsers. The following listing includes a portion of the browser file for Internet Explorer (ie.browser):

<browsers>

<browser id="IE" parentID="Mozilla">

<identification>

<userAgent match="^Mozilla[^(]*([C|c]ompatible;s*MSIE (?'version'(?'major'd+)(?'minor'.d+)(?'letters'w*))(?'extra'[^)]*)" />

<userAgent nonMatch="Opera|Go.Web|Windows CE|EudoraWeb" />

</identification>

<capture></capture>

<capabilities>

<capability name="browser"              value="IE" />

<capability name="extra"                value="${extra}" />

<capability name="isColor"              value="true" />

<capability name="letters"              value="${letters}" />

<capability name="majorversion"         value="${major}" />

<capability name="minorversion"         value="${minor}" />

<capability name="screenBitDepth"       value="8" />

<capability name="type"                 value="IE${major}" />

<capability name="version"              value="${version}" />

</capabilities>

</browser>

<browser id="IE5to9" parentID="IE">

<identification>

<capability name="majorversion" match="^[5-9]" />

</identification>

<capture></capture>

<capabilities>

<capability name="activexcontrols"     value="true" />

<capability name="backgroundsounds"    value="true" />

<capability name="cookies"             value="true" />

<capability name="css1"                value="true" />

<capability name="css2"                value="true" />

<capability name="ecmascriptversion"   value="1.2" />

<capability name="frames"              value="true" />

<capability name="javaapplets"         value="true" />

<capability name="javascript"          value="true" />

<capability name="jscriptversion"      value="5.0" />

<capability name="msdomversion"        value="${majorversion}${minorversion}" />

<capability name="supportsCallback"    value="true" />

<capability name="supportsFileUpload"  value="true" />

<capability name="supportsMultilineTextBoxDisplay" value="true" />

<capability name="supportsMaintainScrollPositionOnPostback" value="true" />

<capability name="supportsVCard"       value="true" />

<capability name="supportsXmlHttp"     value="true" />

<capability name="tables"              value="true" />

<capability name="tagwriter"           value="System.Web.UI.HtmlTextWriter" />

<capability name="vbscript"            value="true" />

<capability name="w3cdomversion"       value="1.0" />

<capability name="xml"                 value="true" />

</capabilities>

</browser>

This excerpt from the browser file for Internet Explorer demonstrates the following points:

  • The root node is the browser's element. It contains one or more browser elements that define the capabilities of browsers included within its realm (like different browser versions).
  • An ID is assigned to this browser definition (IE).
  • The parent browser for this definition is mozilla (parentID attribute), which is defined in the mozilla.browser file.
  • The userAgent element specifies the HTTP user agent string, which identifies this browser. Notice that a regular expression is used to define it.
  • The capabilities element contains children defined as capability elements. These specify various capabilities of the browser. This includes such things as whether it supports tables, callbacks, frames, and so forth.
  • Capabilities for different browser versions are included in other browser elements. The definition for versions five to nine (IE5to9) is included in this listing.

In addition to defining the capabilities of a browser, there is also support for defining control adapters and their behavior within the browser. (I outlined this in my column about CSS Friendly Control Adapters.) Once a browser is laid out, ASP.NET includes a tool for registering it with the environment.

Registering a browser

A key issue of the browser files is that ASP.NET does not have to load or read configuration files every time it requires browser information; ASP.NET provides a command line tool for registering browser setup. After you add a new browser to the setup, you run the AspNet_RegBrowsers tool.

This command line tool creates the required changes that allow the server to access the new browser information. Furthermore, applications do not require shutdown or restart of the Web server to recognize a new browser registration (i.e., browser information is not cached as part of the application). The AspNet_RegBrowsers tool is located in the .NET installation directory:

winntMicrosoft.NETFramework<version>AspNet.RegBrowsers.exe

Choose the coverage

You may also use browser files within the realm of a Web application. This means that you can add specific browser support or behavior to an application so it handles requests from a particular browser in a certain way.

When using Visual Studio, you can easily add a browser file to an application by selecting Add New Item (select Browser File) in a Visual Studio project. Note that browser files must live in a special folder within the ASP.NET application called App_Browsers.

First, create the folder within the application and then you can add browser files to it. A new browser file has the following default format:

<browsers>

<browser id="NewBrowser" parentID="Mozilla">

<identification>

<userAgent match="Unique User Agent Regular Expression" />

</identification>

<capture>

<userAgent match="NewBrowser (?'version'd+.d+)" />

</capture>

<capabilities>

<capability name="browser" value="My New Browser" />

<capability name="version" value="${version}" />

</capabilities>

</browser>

<browser refID="Mozilla">

<capabilities>

<capability name="xml" value="true" />

</capabilities>

</browser>

</browsers>

I can format the file to support a fictitious browser called TechRepublic. The browser gets its functionality from the Mozilla line of browsers, so I use it for the parentID attribute. A unique id is assigned to it (TR). The userAgent element accepts TechRepublic.com along with major and minor version numbers. I did add one element to the capabilities to state that the browser does not support ActiveX controls.

<browsers>

<browser id="TR" parentID="Mozilla">

<identification>

<userAgent match="TechRepublic.com);.(?'version'(?'major'd+)(?'minor'.d+))w*" />

</identification>

<capabilities>

<capability name="browser" value="TR" />

<capability name="activeXControls" value="false" />

</capabilities>

</browser>

</browsers>

Browser support for Web applications

Have you encountered browser issues with your Web applications? Do you use ASP.NET browser files? Share your experience with the .NET community by posting to the article discussion.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

---------------------------------------------------------------------------------------

Get weekly .NET tips in your inbox TechRepublic's free Visual Studio Developer newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as ASP.NET, ADO.NET, and Visual Studio .NET. Automatically subscribe today!

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

4 comments
victoriakor
victoriakor

I had a bit of a problem in printing GridView in Internet Explorer. I needed to print only two rows on one page. The first problem was how to break page. I've found solution at Printing documents in IE

a.p.carter
a.p.carter

I must agree that though detecting the browser version and using custom code, etc to counter such things should be a thing of the past, it does have its uses. If your using ASP.net and ASP.net Ajax controls with the css adapter to make them compliant with the standards there are cases when a page will still fail the W3C validator checks and the only way to avoid such things is to use a .browser file. Below is an example of what is needed to aid the correct rendering of asp.net controls to pass validation.

Justin James
Justin James

In my opinion, browser detection encourages poor habits, like trying to custom code to some users and not others. I had hoped the days of "This site best viewed with..." was behind us. But, for the few times where this is absolutely needed, this is a good way to go about it. :) J.Ja

bbasel
bbasel

If everyone works towards the standards then we all are better off. However in .Net 1.1 the browser definitions were out of date. There was a lot of standard functionallity in Firefox that was disabled by default in .Net 1.1; so things that worked fine in IE did not always work in Firefox due to .Net not know that Firefox supported the functions. I have not looked at the browser definitions in .Net 2.0 but hopefully they are more up to date. If not the browser file functionallity is where we can address these issues.