1

HttpBrowserCapabilities browse = Request.Browser; just seems to bring back IE or Netscape for all other browsers.

I have updated the C:\WINDOWS\system32\inetsrv browscap.ini file on the developer pc that has asp.net c#.

My requirement is to trap the clients browser in server side code.

Thanks.

Hunt
  • 279
  • 1
  • 3
  • 13

4 Answers4

2

http://owenbrady.net/browsercaps/ This is an xml file that you add to your web.config that gives detailed information back to the browsercapabilities structure. This should do what you're looking for.

Edit: I've only tested this on IIS7 and Asp.Net 3.5.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
  • Morder, thank for your help. My requirement is for this to work on .net 1.1 Some seem to say I should update the machine.config others the web.config. If I update the web.config the systems complains. If I update the machine.config the system still complains. All the information out there seem to be unclear. The machine.config already has entries for Firefox so why does the system say the first default option of 'unknown' :( – Hunt Jun 21 '10 at 14:33
  • Is App_Browsers just for .net 2 ? – Hunt Jun 21 '10 at 14:41
  • Ah. I'm sorry, I can't really answer that as I've *only* tested it on IIS7 with ASP.Net 3.5. The web.config method allows you to have it only available on one website vs. the entire machine. http://msdn.microsoft.com/en-us/library/sk9az15a.aspx this page seems to imply that it does work under the requirements and it appears that both web.config and machine.config work but it doesn't really say if there are any limitations as a result of 1.1...I'll keep looking around for you though – Buildstarted Jun 21 '10 at 15:17
  • http://msdn.microsoft.com/en-us/library/ms228122.aspx "Browser definition files were introduced the .NET Framework version 2.0. In earlier versions of the .NET Framework, the browserCaps element was used to define browser definitions in configuration files." – Buildstarted Jun 21 '10 at 15:23
  • Thanks I'm off to look at your links. – Hunt Jun 22 '10 at 13:30
  • http://msdn.microsoft.com/en-us/library/sk9az15a(VS.71).aspx says below is valid for 1.1 and gives an example... The line above creates an error... Parser Error Message: The 'type' attribute must be set to a valid Type name (format: ,) and list the errors... === Pre-bind state information === – Hunt Jun 22 '10 at 14:16
  • The rest of the error is... LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Post-policy reference: System.Web LOG: Attempting download of new URL file:///c:/windows/microsoft.net/framework/v1.1.4322/Temporary ASP.NET Files/webapplication1/0ce05c64/4984f2c7/System.Web.DLL. I cannot understand why there would be problem with the system.web – Hunt Jun 22 '10 at 14:17
  • try this really quick: remove the `type` attribute from the `` tag and see what happens - by default it's set to that type so removing it shouldn't cause any problems and might fix that error - else remove the `,System.Web` portion of the type attribute but off hand, without more research, I couldn't say what's wrong... – Buildstarted Jun 22 '10 at 14:56
  • gives... Type 'System.Web.HttpBrowserCapabilities' does not inherit from 'System.Web.Mobile.MobileCapabilities'. – Hunt Jun 23 '10 at 08:33
  • (remove the type) if you meant then that produces an error that indicates .net does not know what is :( – Hunt Jun 23 '10 at 08:35
  • Just found this... Request.UserAgent it gives the correct response for IE,FireFox,Chrome,safari :) The only problem is parsing the string as can be seen -- User Agent :: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4 -- Chrome has Safari in the string as well :( – Hunt Jun 23 '10 at 11:26
  • Using just the Request.UserAgent .net 1.1 uses the Machine.config in the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG. This can be proven by changing the – Hunt Jun 23 '10 at 13:11
  • Strange, I had that same "does not inherit" error recently but I don't think it was related to the web.config or machine.config. Here's my browser caps that I'm currently using - I can see it doesn't have the same `result type=''` that yours had: http://pastebin.com/QKGe1mdy Not sure if this is going to help but I'm presently out of ideas :) – Buildstarted Jun 23 '10 at 16:02
  • Your browser caps works without any changes (as is) in my web.config. I was not using -> System.Web.Mobile.MobileCapabilities What is the story behind using System.Web.Mobile.MobileCapabilities ? By the way Chrome is stated as Safari this is perhaps because the agent string is Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4 – Hunt Jun 24 '10 at 07:49
  • yeah, someone should update the xml element to support chrome/safari properly - or chrome should stop using "safari". The MobileCapabilities seems to be the one used for .net 1.x while it has been deprecated in later versions. Well I'm glad you got it working to a point. I hope it's what you want or at least works the way you want :) – Buildstarted Jun 24 '10 at 14:22
2

This is the version for 1.1 for Dot.net

http://owenbrady.net/browsercaps/CodeProject.xml

You have to copy it into the proper area in your web.config though.

Owen Brady
  • 81
  • 1
1

Have a look at the App_Browsers folder, you can add a .browser file to expand the detection of the Request.Browser object in ASP.net.

For example: to detect a BlackBerry you need the following rule e.g. blackberry.browser added to the App_Browsers folder:

<browsers>    
  <browser id="BlackBerry" parentID="Default">
    <identification>
      <userAgent match="BlackBerry(?'model'\d+)/(?'version'((?'major'\d+).(?'minor'\d+).(?'other'\d+)))" />
    </identification>
    <capabilities>
      <capability name="browser"     value="BlackBerry" />
      <capability name="isMobileDevice"     value="true" />
      <capability name="javascript"     value="true" />
      <capability name="ecmascriptversion"     value="1.3" />
      <capability name="version"     value="${version}" />
      <capability name="majorVersion"     value="${major}" />
      <capability name="minorVersion"     value="${minor}" />
      <capability name="supportsCss"     value="true" />
      <capability name="css1"                            value="true" />
      <capability name="css2"                                    value="true" />
      <capability name="frames"     value="false" />
      <capability name="cookies"     value="true" />
      <capability name="mobileDeviceManufacturer"   value="RIM" />
      <capability name="isColor"                         value="true" />
      <capability name="supportsBold"                    value="true" />
      <capability name="supportsFontName"                value="true" />
      <capability name="supportsImageSubmit"             value="true" />
      <capability name="supportsItalic"                  value="true" />
      <capability name="preferredImageMime"              value="image/jpeg" />
    </capabilities>
  </browser>
</browsers>

Now you can keep adding your own forever, but there was a project from Microsoft that has thousands of devices in their file: MDBF, but they shut this project down recently (but the last version is still accessible). So the alternative is to include 51degrees.codeplex.com It's not as easy as MDBF was, but should get the job done.

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
1

Using .Net 3.5, I've got HttpBrowserCapabilities working with the browsercaps section of the web.config commented out. Here's what I have, validated in IE 8, Firefox, Chrome, and Safari on the same machine.

        System.Web.HttpBrowserCapabilities browser = Request.Browser;

        BrowserNm.Text = browser.Browser;
        BrowserVer.Text = browser.Version;
        Platform.Text = browser.Platform;

My requirements for browser detection are similar, but we don't want to use browsercaps at all. If you are intent on using browsercaps, you might as well write regular expressions to identify browsers, which was our first option, which I hated.

Three properties I'm still investigating are IsMobileDevice, MobileDeviceManufacturer, and MobileDeviceModel. If i know those work, I'm golden.