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

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

 


                                  

Discussion

  1. Steve Nutt says:

    This has been our single most troublesome issue with ASP.Net and fngers crossed, the new definitions will help solve the majority of browser problems that we encounter.

  2. OmariO says:

    How can I enumerate all browser definitions and their prefixes?

  3. marek says:

    Hi Stephen,

    thanks for sharing.

    How would you compare the mobile browsers definition build in in the ASP.NET and the definitions included in this project:
    http://mdbf.codeplex.com/

    Thanks,
    Marek

  4. Really nice feature for ASP.NET 4.0. Would there be perfromance hit for each incoming request? I can see that you have routing code inside Application_Start() event handler, which actually fires only once when ASP.NET application is started, so I think performance should not be an issue, what say ? Also can I have multiple routes.MapPageRoute() for different URLs inside RegisterRoutes() method ?

  5. waqa says:

    could you please write some articles on difference between 3.5 and 4.0
    and its nice articles though
    thanks

  6. @OmariO — Great question! I updated the blog entry above to contain a new section entitled enumerating browser definitions.

    — Stephen

  7. @marek – I’m a huge fan of the http://mdbf.codeplex.com/ codeplex project. Be aware that the mdbf project only has MOBILE browser definitions and not DESKTOP browser definitions.

    — Stephen

  8. 房屋仲介 says:

    I do not know ,I behind.

  9. Prakash says:

    I am not sure how useful it’d be for desktop browsers, except looking for activeX control support, etc., but it sure would be useful for checking wide variety of mobile features.

  10. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post…

  11. Hosting applications, email, and files for small business. AppsOnDemand provides all of your hosted application and cloud computing needs.

  12. REPLICA HANDBAGS says:

    GLT10a03TAO17
    genanv0201
    |[url=http://www.honeyreplica.com]Replica Handbags[/url]|
    [url=http://www.honeyreplica.com]Replica Louis Vuitton[/url]|
    [url=http://www.iamreplica.com]Replica Watches[/url]|
    [url=http://www.bags-replicas.com]replica handbags[/url]|
    [url=http://www.bags-replicas.com]Louis vuitton handbags[/url]|
    [url=http://www.bagsspace.com]Replica Handbags[/url]|
    [url=http://www.watches9.com]replica watches[/url]|
    [url=http://www.honeyreplica.com]Wholesale Replica Handbags[/url]|
    [url=http://www.hermes-luxury.com]Hermes Handbags[/url]|