Topic: Detecting Mobile Devices
Description: This topic shows you how to detect mobile devices from an ASP.Net web site.
Notes: The IsMobileDevice property on the Request.Browswer object is unreliable, so I would use it.
Below is a method to help you determine if the device calling your page is mobile.
public static bool IsMobile (string requestUserAgent)
{
requestUserAgent = requestUserAgent.ToUpper();
return requestUserAgent.Contains("IPHONE") |
requestUserAgent.Contains("WINDOWS CE") |
requestUserAgent.Contains("PPC") |
requestUserAgent.Contains("PORTABLE") |
requestUserAgent.Contains("PALM") |
requestUserAgent.Contains("OPERA MINI") |
requestUserAgent.Contains("BLACKBERRY") |
requestUserAgent.Contains("MOBILE");
}
Use the line below to do something with the IsMobile return value. In my case, I have a basePage class that all Pages are inherited from, which gives me access to the method above.
protected void Page_PreInit(Object sender, System.EventArgs e)
{
bool thisIsMobile = IsMobile(Request.UserAgent) || Request.Browswer.IsMobileDevice;
MasterPageFile = "~/MyOtherMasterPage.master";
}