« Home | Chart Control for .net 3.5 » | Increase the VS screen for HTML pages » | Working with Dynamic Controls » | Dynamics CRM 4.O SDK for IPHONE » | Sql Injection Tracking Tool » | Using LogParser to get Download Details from IIS » | Getting IP Address and ISP name of our Web Page Vi... » | Error Handling in Web.Config » | Handling Application_Error Method » | Error Handling in .net 3.5 »

Detecting Chrome browser in ASP.NET

ASP.NET comes with a browser capabilities database (BCD), which describes the capabilities of known browsers. This database is usefull for control developers who want to take advantage on different browser capabilities. Of course, more recent browsers are not listed on that database, simply because they did not exist at the time ASP.NET 2.0 was released (versions 3.0, 3.5 and 3.5 SP1 don't add anything new), and, specifically, Chrome is not on that list.

If you want to add new browsers to the BCD, you have two options:

1.The global database, located in the %Windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers directory

2.A local database on your ASP.NET application, in the App_Browsers directory
In any case, you start by adding a new file, say, Chrome.browser, to the appropriate directory. Looking at all browsers registed in the BCD, we choose Safari as the most similar one, and have our definition inherit from it:


<browsers>
<browser id="Chrome" parentID="Safari1Plus">

<identification>
<useragent match="Chrome/(?'version'(?'major'\d+)\.(?'minor'\d+\.\d+).\d+)" />

</identification>

<capabilities>
<capability name="browser" value="Chrome" />

<capability name="majorversion" value="${major}" />
<capability name="minorversion" value="${minor}" />

<capability name="version" value="${version}" />
</capabilities>

</browser>
</browsers>


Of course, you can override the capabilities defined for Safari, if you want, but I think they should be OK. Also, you can add entries for all the Chrome versions.

If you try to access your web application running Chrome and look at the HttpContext.Request.Browser.BrowserName, you may be surprised to see that the name is... not Chrome! It so happens that you must increase the number of characters ASP.NET uses for the user agent header, and you do this in Web.config under <system.web> section.



<browsercaps userAgentCacheKeyLength="128" />



That's it! If you try now, you'll see that the BrowserName property is now "Chrome" and that the listed capabilities match the configured ones.

Labels: , , , ,

Post a Comment