Tuesday, December 9, 2008

How to add support for Google Chrome in ASP.NET

Do you like Google Chrome? I do, especially after they amended the section 11.4 of the EULA

Then what about adding browser capabilities for the browser in ASP.NET. It is better not to modify the browser definition files under the  <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers folder since they can be changed in a next .net framework release or service pack. Just add a .browser file to your web project under the App_Browser folder.

 Here is the content of the file:

  <browser id="GoogleChrome" parentID="Safari1Plus">
      <userAgent match="Chrome/(?'version'(?'major'\d+)\.(?'minor'\d+\.\d+).\d+)" />
      <capability name="browser"        value="Chrome" />
      <capability name="majorversion"   value="${major}" />
      <capability name="minorversion"   value="${minor}" />
      <capability name="version"        value="${version}" />

When I had done it, I found that it appeared a strange behavior. First starting the site in the Chrome made it stick with the Chrome definition for Apple Safari and vise versa.

After playing around with different variations of the above config I decided to stop wasting my time on it and to find how ASP.NET caches the browser capabilities using a reflector. The code of all this is pretty easy. The framework uses the User-Agent request-header as a cache key trimming its length by a configurable parameter.

    <browserCaps userAgentCacheKeyLength="128" />

I extended the value up to 128 from its default of 64 because the Chrome's user-agent string length is 118.



  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.