Extending ASP.NET Browser Capabilities Functionality in asp.net 4.0

The previous section described how to create a new HttpBrowserCapabilities object in ASP.NET 4. You can also extend the ASP.NET browser capabilities functionality by adding new browser capabilities definitions to those that are already in ASP.NET. You can do this without using the XML browser definitions. The following procedure shows how.

1. Create a class that derives from HttpCapabilitiesEvaluator and that overrides the GetBrowserCapabilities method, like the one in the following example:

public class CustomProvider : HttpCapabilitiesEvaluator

{

public override HttpBrowserCapabilities

GetBrowserCapabilities(HttpRequest request)

{

HttpBrowserCapabilities browserCaps =

base.GetHttpBrowserCapabilities(request);

if (browserCaps.Browser == "Unknown")

{

browserCaps = MyBrowserCapabilitiesEvaulator(request);

}

return browserCaps;

}

}

This code first uses the ASP.NET browser capabilities functionality to try to identify the browser. However, if no browser is identified based on the information defined in the request (that is, if the Browser property of the HttpBrowserCapabilities object is the string “Unknown”), the code calls the custom provider (MyBrowserCapabilitiesEvaluator) to identify the browser.

2. Register the provider with the application as described in the previous example.

Extending Browser Capabilities Functionality by Adding New Capabilities to Existing Capabilities Definitions

In addition to creating a custom browser definition provider and to dynamically creating new browser definitions, you can extend existing browser definitions with additional capabilities. This lets you use a definition that is close to what you want but lacks only a few capabilities. To do this, use the following steps.

1. Create a class that derives from HttpCapabilitiesEvaluator and that overrides the GetBrowserCapabilities method, like the one in the following example:

public class CustomProvider : HttpCapabilitiesEvaluator

{

public override HttpBrowserCapabilities

GetBrowserCapabilities(HttpRequest request)

{

HttpBrowserCapabilities browserCaps =

base.GetHttpBrowserCapabilities(request);

browserCaps.Frames = true;

browserCaps.Capabilities["MultiTouch"] = "true";

return browserCaps;

}

}

The example code extends the existing ASP.NET HttpCapabilitiesEvaluator class and gets the HttpBrowserCapabilities object that matched the current request definition by using the following code:

HttpBrowserCapabilities browserCaps =

base.GetHttpBrowserCapabilities(request);

The code can then add or modify a capability for this browser. There are two ways to specify a new browser capability:

· Add a key/value pair to the IDictionary object that is exposed by the Capabilities property of the HttpCapabilitiesBase object. In the previous example, the code adds a capability named MultiTouch with a value of true.

· Set existing properties of the HttpCapabilitiesBase object. In the previous example, the code sets the Frames property to true. This property is simply an accessor for the IDictionary object that is exposed by the Capabilities property.

Note   This model applies to any property of HttpBrowserCapabilities, including control adapters.

2. Register the provider with the application as described in the earlier procedure.

Posted in: asp.net | Tags: asp.net 4.0 broser capabilities httpbrowsercapabilities httpcapabilitiesevaluator getbrowsercapabilities extend key value