Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [183]

By Root 5380 0

Browser Definition Files


The class HttpBrowserCapabilities inherits from HttpBrowserCapabilitiesBase, which represents the list of information that is possible to know about a browser. The base class includes dozens of properties, including IsMobileDevice, SupportsXmlHttp, JScriptVersion, and HasBackButton. As an example, IsMobileDevice returns a Boolean value denoting whether or not the current browser is a mobile device. Likewise, JScriptVersion returns the version of JavaScript currently being supported by the browser, and SupportsXmlHttp indicates whether the browser has AJAX capabilities.

Browser information is read from server-side browser definition files installed with ASP.NET. In ASP.NET 4, you find the following definition files—one for each recognized browser device:

blackberry.browser

chrome.browser

Default.browser

firefox.browser

gateway.browser

generic.browser

ie.browser

iemobile.browser

iphone.browser

opera.browser

safari.browser

Browser definition files are plain XML files located under the following folder:

%Windows%\Microsoft.NET\Framework\v4.0.30319\Config\Browsers

Browser files in the specified folder contain global definitions valid for all applications on the server. If you want to employ application-specific settings, you create an App_Browsers folder in your project and drop into it any .browser file you might need.

At any time, you can add new .browser files or edit any stored information. The syntax of .browser files is a bit quirky, and any edit needs to be conducted by hand, with the risk of breaking things. To make this scenario more seamless, in ASP.NET 4 Microsoft introduced the concept of a browser-capabilities provider.

Note

If you make any edits to any of the .browser files, make sure you re-create the browser assembly in the global assembly cache (GAC). For this to happen, you have to run the following command:

aspnet_regbrowsers.exe -I c

Needless to say, this action will inevitably restart your entire Web application.

Browser Capabilities Providers


In ASP.NET, a provider is a component that implements a contracted interface and interacts with specific ASP.NET subsystems only through that interface. Each ASP.NET subsystem that supports the provider model must have a default provider configured. As a developer, you can make your application switch from one provider to the next declaratively, when not doing it programmatically. Through the provider model, a piece of functionality represented by a “contract” (in this context, it is usually a base class) is injected into a particular subsystem of ASP.NET. Providers exist for membership, role management, user profiles and, in ASP.NET 4, also for managing browser capabilities.

Browser-capabilities providers enforce the following contract:

public abstract class HttpCapabilitiesProvider

{

public abstract HttpBrowserCapabilities GetBrowserCapabilities(HttpRequest request);

}

The default browser-capabilities provider is the class HttpCapabilitiesDefaultProvider you find in the System.Web.Configuration namespace. This class is designed to read browser information from .browser files. Internally, the implementation of the Browser property on the HttpRequest object ends up calling the configured provider and gets to the actual information through the interface of the HttpCapabilitiesProvider class.

If you need to read browser information from other sources, you can replace or extend the default provider. You create a new provider class that derives from HttpCapabilitiesProvider and overrides the GetBrowserCapabilities method:

public class CustomProvider : HttpCapabilitiesProvider

{

public override HttpBrowserCapabilities GetBrowserCapabilities(HttpRequest request)

{

// Detect the browser

var userAgent = request.UserAgent;

// Retrieve information

var values = GetBrowserInfoAsHashTable(userAgent);

// Pack information in a consumable format

var browserCaps = new HttpBrowserCapabilities();

browserCaps.Capabilities = values;

return browserCaps;

Return Main Page Previous Page Next Page

®Online Book Reader