9 Kasım 2013 Cumartesi

Mobil ise Browser yönlendirme

ASP.NET 4.0 ile aşağıdaki kod yardımıyla mobil kullanıcıları algılayabilir ve uygulamanın mobil sayfasına yönlendirebilirsiniz;



private static readonly Regex MobileRegex = new Regex(@"(nokia|sonyericsson|blackberry|IPHONE|samsung|sec-|windows ce|motorola|mot-|up.b|midp-)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

public bool IsMobile
{
 get
 {
  HttpRequest r = HttpContext.Current.Request;

  if (r.Browser.IsMobileDevice)
   return true;

  if (!string.IsNullOrEmpty(r.UserAgent) && MobileRegex.IsMatch(r.UserAgent))
   return true;

  return false;
 }
}

protected void Page_Load(object sender, EventArgs e)
{
 if (IsMobile)
  Response.RedirectPermanent("Mobile.aspx", true);
}
Sayfanın bir mobil cihazda görüntülenebilir olduğunu bildirmek için  tagi arasında şu meta etiketleri gereklidir: