Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect tablet(any) requests?

Tags:

c#

asp.net

 if (Request.Browser.IsMobileDevice)
 {
     Response.Redirect("/mobile/Login.htm");`
 }

To detect mobiles but same times it detect Tablet like mobile, i need function that check if there Tablet or function that check the size of screen of the device.

Thank"s that work i used ScreenPixelsWidth and ScreenPixelsHeight this is the code if any need it

 int wight = Request.Browser.ScreenPixelsWidth;
                int height = Request.Browser.ScreenPixelsHeight;

                if (Request.Browser.IsMobileDevice && wight < 720 && height<1280)
            {
               Response.Redirect("/mobile/Login.htm");
            }
like image 267
Vladimir Potapov Avatar asked Feb 13 '13 10:02

Vladimir Potapov


People also ask

How can I tell if a mobile request came from?

You get a header / message from a device. All you know about the device is in the header and the device can write what it wants in it. If you are talking about http requests (which is indicated by agent lookup) you can look at a header here: All you can do "reliable" is to look for the user agent.


3 Answers

ScreenPixelsWidth always returns 640 so is not useful in detecting phones. I found this works:

public static bool IsPhoneDevice(this HttpBrowserCapabilitiesBase Browser)
{
      return (Browser.IsMobileDevice && Browser.CanInitiateVoiceCall);
}
like image 119
Maximvs Avatar answered Nov 15 '22 15:11

Maximvs


I had a similar issue and tried using: HttpContext.Request.Browser.ScreenPixelsWidth

However this always returned a value of 640 pixels regardless of device (iphone or ipad). I resolved the issue by creating a static method to inspect the User Agent string instead.

public class DeviceHelper
{
    public static bool IsTablet(string userAgent, bool isMobile)
    {
        Regex r = new Regex("ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle|nexus");
        bool isTablet = r.IsMatch(userAgent) && isMobile;
        return isTablet;
    }
}

Then in my controller:

if(DeviceHelper.IsTablet(Request.UserAgent, Request.Browser.IsMobileDevice))
     return Redirect("..."); // redirect to tablet url
like image 24
Victor Lopez Jr. Avatar answered Nov 15 '22 13:11

Victor Lopez Jr.


You can use ScreenPixelsWidth and ScreenPixelsHeight (http://msdn.microsoft.com/en-us/library/system.web.httpbrowsercapabilities.aspx) and you can define a threshold in which you consider whether the regular or the mobile version should be rendered.

There are many more ways to tackle this issue but since you are already using the HttpBrowserCapabilities class, you might as well use these 2 properties.

like image 35
Icarus Avatar answered Nov 15 '22 13:11

Icarus