Browser and Operating System Detection In ASP.NET

26 בינואר 2009

12 comments

Hi,


Detect user Browser and Operating System version in ASP.NET is very simple using the C# code:



Request.Browser


This code retuns the HttpBrowserCapabilities class that has the information we desire.


Unfortunately the default Browser Caps configuration that come with ASP.NET 2.0 does not include browsers like Google Chrome and I could not find a way of getting the operating system version.


Finally I came up with solution that solve my problem as describe below:



  • On my ASP.NET application I open new ASP.NET folder: App_Browsers.

  • I download updated *.browser files that contain the information on the Google Chrome browser and also the operating system version and copy them to the App_Browsers folder. You can get the updated files from here.

I also attach small ASP.NET project that explain the solution. You can download it from here.


Enjoy Rotem 🙂

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

12 comments

  1. P.S.30 בינואר 2009 ב 18:41

    Thanks for sharing this tech

    Reply
  2. Christian12 בספטמבר 2009 ב 0:58

    Hello Rotem,

    I get an error with last version in your test project:
    Erreur du serveur dans l'application '/'.
    ——————————————————————————–

    Erreur d'analyse
    Description : Une erreur s'est produite au cours de l'analyse d'une ressource requise pour répondre à cette demande. Veuillez consulter ci-dessous les détails relatifs à l'erreur d'analyse en question, puis modifier votre fichier source de manière appropriée.

    Message d'erreur de l'analyseur: La référence d'objet n'est pas définie à une instance d'un objet.

    Erreur source:

    [Aucune ligne source appropriée]

    Fichier source : aucun Ligne : 1

    ——————————————————————————–
    Informations sur la version : Version Microsoft .NET Framework :2.0.50727.4016; Version ASP.NET :2.0.50727.4016

    Reply
  3. Rotem Bloom13 בספטמבר 2009 ב 10:26

    Christian,
    I need to see the error on English 😉

    Reply
  4. Ashok20 באפריל 2010 ב 13:03

    I ran the test project that you have provided and it ran fine, detecting the chrome browser. So, i added the two .browser files from the AppBrowser folder to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers".
    Then i ran my web app and then it is not detecting chrome. It is detecting chrome browser from your sample application only. Why is it so?

    Reply
  5. Rotem Bloom20 באפריל 2010 ב 14:19

    Ashok,
    Can you send me a demo of your application?

    Reply
  6. Ashok21 באפריל 2010 ב 9:46

    Hi Rotem,
    Its nothing but a mere Response.Write(Request.Browser.Browser) in an aspx.cs page.

    Reply
  7. Rotem Bloom21 באפריל 2010 ב 11:23

    Ashok,
    Why did you put the files under:
    "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers"
    Just do what I did on my demo application, put it under: App_Browsers folder of your ASP.NET application.
    Hope it helps,
    Rotem

    Reply
  8. Ashok26 באפריל 2010 ב 8:45

    Ours is a sharepoint application and we don't have a web project for aspx pages; instead there is a class library project. Therefore i can't add an App_Browser folder.

    Reply
  9. Rotem Bloom26 באפריל 2010 ב 11:52

    Hi Ashok,
    So I think the problem is around sharepoint. try to add the root directory of the sharepoint application.
    Maybe you can convert or upgrade the sharepoint to support web project.

    Bye
    Rotem

    Reply
  10. Jonathan19 בינואר 2012 ב 22:44

    Thanks a lot finally an actual .browser file that actually works in asp.net 2.

    All the other example I found to detect Chrome failed. I guess due to the file since they all used the same.

    I was beginning to think my app was ignoring my App_Browser folder until I tried your OceanOS.browser file.

    Reply
  11. buga9 באפריל 2012 ב 16:44

    Although this is great solution it is not working for .NET 4.0. Do you have any fix for this issue?

    Reply
  12. Rotem Bloom9 באפריל 2012 ב 17:24

    On .net 4.0 it should work this solution is only for 2.0

    Reply