locked
Detecting mobile device user agents in ASP.NET (Android) RRS feed

  • Question

  • User-1831219222 posted

    Hi,

    Last week Android updated their software (changed the display from 480px to 800px) so all the mobile tweaks we made for our apps are no longer work on Droid Phones.

    I would need to identify the User Agent(e.g. Android) and switch style sheets depending. (We don't want to redirect to another page, just switch style sheets).

    Anybody know any lightweight solution ?

    Thanks in Advance.

    (we were using 480px pixels to identify if the user was coming from iPhone or Android and switched stylesheets accordingly.  If I changed it to 800px some desktop monitors and the iPad would see the mobile version and not the desktop version. Not good.)

     

     

    Thursday, August 26, 2010 3:03 PM

Answers

  • User-206222649 posted

    Request.UserAgent.Contains("Android");


    Let me elaborate:


    <% if(Request.UserAgent.contains("Android"){%>

    include android CSS here

    <%}else{%>

    include iPhone css here

    <%}%>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 26, 2010 3:23 PM
  • User-1831219222 posted

    This code solved my problem. Thanks to all.

      //for Mobile device
        protected override void OnInit(EventArgs e)
        {
          
            string userAgent = Request.UserAgent;
            if (userAgent.Contains("BlackBerry")
              || (userAgent.Contains("iPhone") || (userAgent.Contains("Android"))))
            {
                //add css ref to header from code behind
                HtmlLink css = new HtmlLink();
                css.Href = ResolveClientUrl("~/mobile.css");
                css.Attributes["rel"] = "stylesheet"; 
                css.Attributes["type"] = "text/css"; 
                css.Attributes["media"] = "all"; 
                Page.Header.Controls.Add(css);
            }      
        }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 12:08 PM

All replies

  • User-206222649 posted

    Request.UserAgent.Contains("Android");


    Let me elaborate:


    <% if(Request.UserAgent.contains("Android"){%>

    include android CSS here

    <%}else{%>

    include iPhone css here

    <%}%>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 26, 2010 3:23 PM
  • User-1831219222 posted

    Thanks Evan for your reply.

    Android changed the display 480px to 800 px. I was using 480px pixels to identify if the user was coming from iPhone or Android and switched stylesheets accordingly. 

    So, can I just neglect that pixels thing to identify the device and follow your suggestion ?

    Thanks

     

     

    Thursday, August 26, 2010 5:14 PM
  • User981004358 posted

    The latest capabilities list which is available in WURFL is read using 51degrees.mobi Foundation API which detects all mobile devices. It is a .NET open source available here http://51degrees.codeplex.com/releases.

    It easily detects if the request is coming from any mobile device and redirects user to mobile landing page. It has some user friendly samples to try out.

    There is a 4 minute video which shows how to Enhance your existing website to contain mobile pages.

    Video (3:43 minutes): http://www.youtube.com/watch?v=b4io5KB4Qt0

     

    Hope this helps

    - Thank You

    Friday, August 27, 2010 9:46 AM
  • User-1831219222 posted

    This code solved my problem. Thanks to all.

      //for Mobile device
        protected override void OnInit(EventArgs e)
        {
          
            string userAgent = Request.UserAgent;
            if (userAgent.Contains("BlackBerry")
              || (userAgent.Contains("iPhone") || (userAgent.Contains("Android"))))
            {
                //add css ref to header from code behind
                HtmlLink css = new HtmlLink();
                css.Href = ResolveClientUrl("~/mobile.css");
                css.Attributes["rel"] = "stylesheet"; 
                css.Attributes["type"] = "text/css"; 
                css.Attributes["media"] = "all"; 
                Page.Header.Controls.Add(css);
            }      
        }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 12:08 PM