Replacing the ASP.NET Browser Capabilities Functionality in asp.net 4.0
To replace the ASP.NET browser capabilities definition functionality completely, follow these steps:
1. Create a provider class that derives from HttpCapabilitiesProvider and that overrides the GetBrowserCapabilities method, as in the following example:
public class CustomProvider : HttpCapabilitiesProvider
{
public override HttpBrowserCapabilities
GetBrowserCapabilities(HttpRequest request)
{
HttpBrowserCapabilities browserCaps = new HttpBrowserCapabilities();
Hashtable values = new Hashtable(180, StringComparer.OrdinalIgnoreCase);
values[String.Empty] = request.UserAgent;
values["browser"] = "MyCustomBrowser";
browserCaps.Capabilities = values;
return browserCaps;
}
}
The code in this example creates a new HttpBrowserCapabilities object, specifying only the capability named browser and setting that capability to MyCustomBrowser.
2. Register the provider with the application.
In order to use a provider with an application, you must add the provider attribute to the browserCaps section in the Web.config or Machine.config files. (You can also define the provider attributes in a location element for specific directories in application, such as in a folder for a specific mobile device.) The following example shows how to set the provider attribute in a configuration file:
<system.web>
<browserCaps provider="ClassLibrary2.CustomProvider, ClassLibrary2,
Version=1.0.0.0, Culture=neutral" />
</system.web>
Another way to register the new browser capability definition is to use code, as shown in the following example:
void Application_Start(object sender, EventArgs e)
{
HttpCapabilitiesBase.BrowserCapabilitiesProvider =
new ClassLibrary2.CustomProvider();
// ...
}
This code must run in the Application_Start event of the Global.asax file. Any change to the BrowserCapabilitiesProvider class must occur before any code in the application executes, in order to make sure that the cache remains in a valid state for the resolved HttpCapabilitiesBase object.
Caching the HttpBrowserCapabilities Object
The preceding example has one problem, which is that the code would run each time the custom provider is invoked in order to get the HttpBrowserCapabilities object. This can happen multiple times during each request. In the example, the code for the provider does not do much. However, if the code in your custom provider performs significant work in order to get the HttpBrowserCapabilities object, this can result in huge overhead. To prevent this from happening, you can cache the HttpBrowserCapabilities object. Follow these steps:
1. Create a class that derives from HttpCapabilitiesProvider, like the one in the following example:
public class CustomProvider : HttpCapabilitiesProvider
{
public override HttpBrowserCapabilities
GetBrowserCapabilities(HttpRequest request)
{
string cacheKey = BuildCacheKey();
int cacheTime = GetCacheTime();
HttpBrowserCapabilities browserCaps =
HttpContext.Current.Cache[cacheKey] as
HttpBrowserCapabilities;
if (browserCaps == null)
{
HttpBrowserCapabilities browserCaps = new
HttpBrowserCapabilities();
Hashtable values = new Hashtable(180,
StringComparer.OrdinalIgnoreCase);
values[String.Empty] = request.UserAgent;
values["browser"] = "MyCustomBrowser";
browserCaps.Capabilities = values;
HttpContext.Current.Cache.Insert(cacheKey,
browserCaps, null, DateTime.MaxValue,
TimeSpan.FromSeconds(cacheTime));
}
return browserCaps;
}
}
In the example, the code generates a cache key by calling a custom BuildCacheKey method, and it gets the length of time to cache by calling a custom GetCacheTime method. The code then adds the resolved HttpBrowserCapabilities object to the cache. The object can be retrieved from the cache and reused on subsequent requests that need your custom provider.
2. Register the provider with the application as described in the preceding procedure.
Posted in: asp.net | Tags: asp.net asp.net 4.0 replace broser capabilities functionality httpcapabilitiesprovider httpbrowsercapabilitiesExtending 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