Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

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 that 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:

<system.web>

    <browserCaps userAgentCacheKeyLength="128" />

    ...

And 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.

Bookmark and Share
Posted: Sep 22 2009, 06:43 PM by Ricardo Peres | with 8 comment(s)
Filed under: ,

Comments

Will said:

Many thanks for this. Its easy to implement and works perfectly.

# January 19, 2010 7:28 AM

vforvijay said:

many thanks..i am looking for this one....

# May 31, 2011 8:23 AM

ashish.net1 said:

I add Chrome.browser file in %Windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers directory.

and also add <browserCaps userAgentCacheKeyLength="128" /> tag in web.config file and build solution.

But when i debug my code and view value on server side to get browser name (Request.Browser.Browser in c#) i get "AppleMAC-Safari" which is totally wrong.

Is there anything missing from my side? Please give me your suggestion to get browser name as chrome in c#.

# June 23, 2011 11:03 AM

ashish.net1 said:

also see this site:

www.codemonkeez.com/.../detect-google-chrome-browser-with.html

where you get, how to store browser details in local project directory.

# June 23, 2011 11:32 AM

Ricardo Peres said:

@ashish.net1:

Try this:

<browsers>

   <browser id="Chrome" parentID="Mozilla">

       <identification>

           <userAgent match="Chrome/(?'version'(?'major'\d+)(\.(?'minor'\d+)?)\w*)" />

       </identification>

       <capture>

           <userAgent match="AppleWebKit/(?'layoutVersion'\d+)" />

       </capture>

       <capabilities>

           <capability name="layoutEngine"                    value="WebKit" />

           <capability name="layoutEngineVersion"             value="${layoutVersion}" />

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

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

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

           <capability name="type"                            value="Chrome${major}" />

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

           <capability name="ecmascriptversion"               value="3.0" />

           <capability name="javascript"                      value="true" />

           <capability name="javascriptversion"               value="1.7" />

           <capability name="w3cdomversion"                   value="1.0" />

           <capability name="supportsAccesskeyAttribute"      value="true" />

           <capability name="tagwriter"                       value="System.Web.UI.HtmlTextWriter" />

           <capability name="cookies"                         value="true" />

           <capability name="frames"                          value="true" />

           <capability name="javaapplets"                     value="true" />

           <capability name="supportsCallback"                value="true" />

           <capability name="supportsDivNoWrap"               value="false" />

           <capability name="supportsFileUpload"              value="true" />

           <capability name="supportsMaintainScrollPositionOnPostback" value="true" />

           <capability name="supportsMultilineTextBoxDisplay" value="true" />

           <capability name="supportsXmlHttp"                 value="true" />

           <capability name="tables"                          value="true" />

       </capabilities>

   </browser>

</browsers>

The other guy copied from my post...

# June 23, 2011 3:03 PM