Use ASP.NET 4 Browser Definitions with ASP.NET 3.5

Posted by Stephen Walther on Stephen Walter See other posts from Stephen Walter or by Stephen Walther
Published on Sat, 06 Mar 2010 00:22:16 GMT Indexed on 2010/03/20 0:51 UTC
Read the original article Hit count: 1466

Filed under:

We updated the browser definitions files included with ASP.NET 4 to include information on recent browsers and devices such as Google Chrome and the iPhone. You can use these browser definition files with earlier versions of ASP.NET such as ASP.NET 3.5. The updated browser definition files, and instructions for installing them, can be found here:

http://aspnet.codeplex.com/releases/view/41420

The changes in the browser definition files can cause backwards compatibility issues when you upgrade an ASP.NET 3.5 web application to ASP.NET 4. If you encounter compatibility issues, you can install the old browser definition files in your ASP.NET 4 application. The old browser definition files are included in the download file referenced above.

What’s New in the ASP.NET 4 Browser Definition Files

The complete set of browsers supported by the new ASP.NET 4 browser definition files is represented by the following figure:

 

clip_image002

 

If you look carefully at the figure, you’ll notice that we added browser definitions for several types of recent browsers such as Internet Explorer 8, Firefox 3.5, Google Chrome, Opera 10, and Safari 4.

Furthermore, notice that we now include browser definitions for several of the most popular mobile devices: BlackBerry, IPhone, IPod, and Windows Mobile (IEMobile). The mobile devices appear in the figure with a purple background color. To improve performance, we removed a whole lot of outdated browser definitions for old cell phones and mobile devices.

We also cleaned up the information contained in the browser files. Here are some of the browser features that you can detect:

    Are you a mobile device?
    <%=Request.Browser.IsMobileDevice %>

    
Are you an IPhone? <%=Request.Browser.MobileDeviceModel == "IPhone" %>
What version of JavaScript do you support? <%=Request.Browser["javascriptversion"] %>
What layout engine do you use? <%=Request.Browser["layoutEngine"] %>

 

Here’s what you would get if you displayed the value of these properties using Internet Explorer 8:

clip_image004

Here’s what you get when you use Google Chrome:

clip_image006

Testing Browser Settings

When working with browser definition files, it is useful to have some way to test the capability information returned when you request a page with different browsers. You can use the following method to return the HttpBrowserCapabilities the corresponds to a particular user agent string and set of browser headers:

public HttpBrowserCapabilities GetBrowserCapabilities(string userAgent, NameValueCollection headers)
{
     HttpBrowserCapabilities browserCaps = new HttpBrowserCapabilities();
     Hashtable hashtable = new Hashtable(180, StringComparer.OrdinalIgnoreCase);
     hashtable[string.Empty] = userAgent; // The actual method uses client target
     browserCaps.Capabilities = hashtable;

     var capsFactory = new System.Web.Configuration.BrowserCapabilitiesFactory();
     capsFactory.ConfigureBrowserCapabilities(headers, browserCaps);
     capsFactory.ConfigureCustomCapabilities(headers, browserCaps);
     return browserCaps;
}

At the end of this blog entry, there is a link to download a simple Visual Studio 2008 project – named Browser Definition Test -- that uses this method to display capability information for arbitrary user agent strings. For example, if you enter the user agent string for an iPhone then you get the results in the following figure:

clip_image008

The Browser Definition Test application enables you to submit a user-agent string and display a table of browser capabilities information.

The browser definition files contain sample user-agent strings for each browser definition. I got the iPhone user-agent string from the comments in the iphone.browser file.

Enumerating Browser Definitions

Someone asked in the comments whether or not there is a way to enumerate all of the browser definitions. You can do this if you ware willing to use a little reflection and read a private property.

The browser definition files in the config\browsers folder get parsed into a class named BrowserCapabilitesFactory. After you run the aspnet_regbrowsers tool, you can see the source for this class in the config\browser folder by opening a file named BrowserCapsFactory.cs.

The BrowserCapabilitiesFactoryBase class has a protected property named BrowserElements that represents a Hashtable of all of the browser definitions. Here's how you can read this protected property and display the ID for all of the browser definitions:

var propInfo = typeof(BrowserCapabilitiesFactory).GetProperty("BrowserElements", BindingFlags.NonPublic | BindingFlags.Instance);
Hashtable browserDefinitions = (Hashtable)propInfo.GetValue(new BrowserCapabilitiesFactory(), null);

foreach (var key in browserDefinitions.Keys)
{
   Response.Write("
" + key); }

If you run this code using Visual Studio 2008 then you get the following results:

image

You get a huge number of outdated browsers and devices. In all, 449 browser definitions are listed.

If you run this code using Visual Studio 2010 then you get the following results:

image

In the case of Visual Studio 2010, all the old browsers and devices have been removed and you get only 19 browser definitions.

Conclusion

The updated browser definition files included in ASP.NET 4 provide more accurate information for recent browsers and devices. If you would like to test the new browser definitions with different user-agent strings then I recommend that you download the Browser Definition Test project:

Browser Definition Test Project

 

 

© Stephen Walter or respective owner

Related posts about ASP.NET