Search code examples
c#asp.net.netbrowser-detection

Browser detection in C# (.Net)


I'm in C# land today. I'm trying to write a function which accepts a user agent string and returns an object that gives me at least the browser name and version. So I tried this answer, but apparently I don't have access to HttpBrowserCapabilities. It tells me the type or namespace name could not be found (yes, even if I add using System.Web, it still doesn't show up, or when I type using System.Web. it doesn't pop up, so it's obviously not there).

I'm using .net 3.5, but the documentation for that class shows it existed even in 3.5, so I'm not sure what's going on. I have access to the browscap files - ini or xml. Am I going to have to write this from scratch?

Edit: I've fixed the reference problem. But Chrome is being reported as AppleMAC-Safari 5.0. I'm wondering if I'm going to need a completely different approach. PHP figures it out with the same ini file fine.


Solution

  • Adding a using block does not automatically import the DLL. All a using does is allow you to not write:

    System.Web.HttpClient //Or whatever
    

    All over the place, and use HttpClient instead.

    You need to add a reference to System.Web to the project before any of its classes will be available to you.