Implement your own IsMobile check with handy dev functionality
Example code of IsMobile (determining device), with some handy dev functionality, see comments in code snippet.
public Boolean IsMobile
{
get
{
if (HttpContext.Current == null)
return false;
//Making it possible to put ?showResponsive=true on any page to see it Mobile
if (HttpContext.Current.Request.QueryString["showResponsive"] + "" == "true")
return true;
//Activate Mobile Channel by having an appsetting, to configure different behavior for test/dev purposes.
//values cookie, mobiles, browsername
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["ResponsiveBrowser"]))
{
//you can have a cookie set to display the mobile channel
HttpCookie c = HttpContext.Current.Request.Cookies.Get("ResponsiveBrowser");
if (c == null)
c = new HttpCookie("ResponsiveBrowser", "false");
DeviceType deviceType = GetDevieType(HttpContext.Current.Request.UserAgent);
if (HttpContext.Current.Request.Browser.Type.Contains(
ConfigurationManager.AppSettings["ResponsiveBrowser"]))
{
return true;
}
else if (ConfigurationManager.AppSettings["ResponsiveBrowser"].ToLower() == "cookie" &&
HttpContext.Current.Request.Cookies.Get("ResponsiveBrowser") != null &&
HttpContext.Current.Request.Cookies.Get("ResponsiveBrowser").Value == "true")
{
return true;
}
else if ((ConfigurationManager.AppSettings["ResponsiveBrowser"].ToLower() == "mobiles" &&
deviceType == DeviceType.Phone && HttpContext.Current.Request.Cookies.Get("ResponsiveBrowser") == null) || (ConfigurationManager.AppSettings["ResponsiveBrowser"].ToLower() == "mobiles" && c.Value == "true"))
{
return true;
}
return HttpContext.Current.Request.Browser.IsMobileDevice;
}
return false;
}
}
public static DeviceType GetDevieType(string userAgent)
{
if (!String.IsNullOrEmpty(userAgent))
{
if (userAgent.ToLowerInvariant().Contains("blackberry")
|| userAgent.ToLowerInvariant().Contains("iphone"))
return DeviceType.Phone;
if (userAgent.ToLowerInvariant().Contains("ipad"))
return DeviceType.Tablet;
if (userAgent.ToLowerInvariant().Contains("android"))
{
if (userAgent.ToLowerInvariant().Contains("mobile"))
return DeviceType.Phone;
else
return DeviceType.Tablet;
}
if (userAgent.ToLowerInvariant().Contains("windows nt") && userAgent.ToLowerInvariant().Contains("touch"))
{
return DeviceType.Tablet;
}
if (userAgent.ToLowerInvariant().Contains("windows") && userAgent.ToLowerInvariant().Contains("touch"))
{
return DeviceType.Phone;
}
}
return DeviceType.Desktop;
}
public enum DeviceType
{
Desktop,
Tablet,
Phone
}