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");
}
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.
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);
}
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With