You are on page 1of 5

Changes in Browser Capabilities with ASP.

NET 4
ASP.NET 4 has been updated to include information for modern browsers and devices. You can now use ASP.NET 4 to detect features of the following browsers and devices: Microsoft Internet Explorer 6, 7, 8 Google Chrome Mozilla Firefox 3, 3.5 Apple Safari 3, 4 Opera 8, 9, 10 IPhone, IPod Blackberry Windows Mobile

To improve performance, information for older browsers and devices was removed. For example, ASP.NET 4 no longer includes browser information for Netscape Navigator or versions of Internet Explorer older than Internet Explorer 6.0. This article explains how you can use the new browser capabilities files with earlier versions of ASP.NET such as ASP.NET 3.5. This article also explains how you can revert to using the ASP.NET 3.5 browser files with ASP.NET 4.

Browser Definition Files and Browser Capabilities


ASP.NET stores information about browsers and devices in a set of files named the browser definition files. These files are simple XML files (you can open them in Notepad). Earlier versions of ASP.NET, such as ASP.NET 3.5, use the browser definition files located in the following directory: \Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers ASP.NET 4 uses the browser definition files located in the following directory: \Windows\Microsoft.NET\Framework\v4.xxx\CONFIG\Browsers

Each browser definition file contains a set of browser definitions. For example, the firefox.browser file contains browser definitions for Firefox 3 and Firefox 3.5. You can learn about the structure of a browser definition file by reading the following article located at the MSDN website: http://msdn.microsoft.com/en-us/library/ms228122.aspx The HttpBrowserCapabilities object exposed by the Request.Browser property -uses these browser definition files both to detect and to return information about the capabilities of different browsers. For example, you can check the Request.Browser.Cookies property to detect whether the current browser supports browser cookies. Web controls, such as the Menu and TreeView controls, use browser capabilities information to adaptively render different script and markup for different types of browsers. For example, the experience of using a TreeView control with a browser that does not support JavaScript is different than the experience of using a TreeView with a browser that does support JavaScript.

Using ASP.NET 4 Browser Definition Files with Earlier Versions of ASP.NET


You can use the new browser definition files included with ASP.NET 4 with earlier versions of ASP.NET such as ASP.NET 3.5. Youll want to take advantage of the new browser definition files, for example, if you want to correctly identify Google Chrome or the IPhone. The ASP.NET 4 browser definition files are included with this article in a folder named ASP.NET 4 Browser Definition Files. Follow these steps to use the new definition files with an ASP.NET 3.5 (or earlier) web application: 1. Replace the contents of your \Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\ folder with the contents of the ASP.NET 4 Browser Definition Files folder. The ASP.NET 4 Browser Definition Files folder is included with this article. Ensure that the only files in the Config folder are the new browser definition files.

2. Open the Visual Studio 2008 Command Prompt (or the Visual Studio 2005 Command Prompt) and execute the following command:

aspnet_regbrowsers i

3. Perform a Rebuild of your Visual Studio project by selecting the menu option Build, Rebuild. After you complete these steps, your ASP.NET application will use the new browser definition files.

Using ASP.NET 3.5 Browser Definition Files with ASP.NET 4


Because the browser definition files in ASP.NET 4 are not the same as the browser definition files in earlier versions of ASP.NET, upgrading an ASP.NET 3.5 web application to ASP.NET 4 can introduce backwards compatibility issues. In this case, you might want to revert back to the browser definition files included in earlier versions of ASP.NET. The ASP.NET 3.5 browser definition files are included with this article in a folder named Old Browser Definition Files. Follow these steps to use the original browser definition files with an ASP.NET 4 application: 1. Replace the contents of your \Windows\Microsoft.NET\Framework\v4.xxx\CONFIG\ folder with the contents of the Old Browser Definition Files folder. The Old Browser Definition Files folder is included with this article. Ensure that the only files in the Config folder are the old browser definition files.

2. Open the Visual Studio 2010 Command Prompt and execute the following command:

aspnet_regbrowsers i

3. Perform a Rebuild of your Visual Studio project by selecting the menu option Build, Rebuild. After you complete these steps, your ASP.NET 4 applications will use the original browser definition files used with ASP.NET 3.5 and earlier versions of ASP.NET.

Custom Browser Definition Files


You can create a custom browser definition file and use it with a particular ASP.NET application by adding the custom browser definition file to the App_Browsers folder. If your application contains custom browser definitions that inherit from one of the browser definitions that have been removed, you will see an error. For example, if the App_Browsers folder contains a browser definition that inherits from the IE2 browser definition, you will receive the following configuration error message: The browser or gateway element with ID 'IE2' cannot be found. Figure 1 represents the relationship between all of the browser definitions in the new ASP.NET 4 browser definition files. All of the browser definitions ultimately inherit from the Default browser definition. Most of the browser definitions (with the exception of Opera and BlackBerry) inherit from the Mozilla browser definition. If, previously, you inherited your custom browser definition file from the IE2 browser definition then you would most likely want to change your custom browser definition to inherit from the IE browser definition.

Figure 1 Browser Definition Inheritance

Detecting the Browser Definition Files Version


You can detect the version of the browser definition files being used by an ASP.NET application by testing the application with a recent browser such as IE 8 or Firefox 3.5. Add the following expression to the body of an ASP.NET page:
<%=Request.Browser.Id %>

If you open the page using Microsoft Internet Explorer 8, and you are using the new browser definition files, then you will see the identifier ie8. Otherwise, if your application is using the older browser definition files, then you will seethe identifier ie6to9.

Summary
In this article, you learned how you can use the ASP.NET 4 browser definition files with earlier versions of ASP.NET. By taking advantage of the updated browser files, you can detect recent browsers such as Google Chrome and the IPhone. You also learned how you can revert back to the old browser definitions files when building ASP.NET 4 Web applications. If you encounter backwards compatibility issues when upgrading from ASP.NET 3.5 to ASP.NET 4, you can revert back to the earlier behavior.

You might also like