Check for Mobile Devices
CodeKeep C# Feed Novembre 29th, 2007
Description: Check for Mobile DevicesLink: http://www.codekeep.net/snippets/645b1297-ac91-4060-9ef9-c09369a8befc.aspx
<add key="MobileDevices" value="(nokia|sonyericsson|blackberry|samsung|sec-|windows ce|motorola|mot-|up.b)" />
Then I wrote a property called IsMobile which tries the Request.Browser.IsMobileDevice property first, and then tries the regular expression afterwards.
private static readonly Regex MOBILE_REGEX = new Regex(ConfigurationManager.AppSettings.Get("MobileDevices"), RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static bool IsMobile
{
get
{
HttpContext context = HttpContext.Current;
if (context != null)
{
HttpRequest request = context.Request;
if (request.Browser.IsMobileDevice)
return true;
if (!string.IsNullOrEmpty(request.UserAgent) && MOBILE_REGEX.IsMatch(request.UserAgent))
return true;
}
return false;
}
}