Mobil Cihazlardan Gelen Kullanicilarimizi Farkli Sayfaya Yonlendirme

Bu pazar gunu yapilacak yerel secimler icin bir micro site hazirladik. Bu sitemizde yazilim ekibimizin yogun olmasindan dolayi banada mobil cihazlardan gelen istekleri mobil cihazlar icin tasarladigimiz sayfaya yonledirme gorevi dustu. Biraz arastirmadan sonra asagidaki kodu yazdim.

   
   1:   private static Regex MOBILE_REGEX = new Regex(@"(nokia|sonyericsson|blackberry|IPHONE|samsung|sec-|windows ce|motorola|mot-|up.b|midp-)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
   2:      
   3:      public static bool IsMobile
   4:      {
   5:          get
   6:          {
   7:              HttpContext context = HttpContext.Current;
   8:              if (context != null)
   9:              {
  10:                  HttpRequest request = context.Request;
  11:                  if (request.Browser.IsMobileDevice)
  12:                  {
  13:                      return true;
  14:                  }
  15:                  if (!string.IsNullOrEmpty(request.UserAgent) && MOBILE_REGEX.IsMatch(request.UserAgent))
  16:                     {
  17:                          return true;
  18:                     }
  19:              }
  20:              return false;
  21:          }
  22:      }
  23:   
  24:      protected void Page_Load(object sender, EventArgs e)
  25:      {
  26:          if (IsMobile)
  27:          {
  28:              Response.Redirect("http://www.ntvmsnbc.com/mobile.aspx");
  29:          }
  30:      }

Category: ASP.NET  Tags: , ,
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
One Response
  1. Sefa diyor ki:

    Kolay gelsin Durgut abicim yazdığın yazı çok önemli ve ileride çok ie yarıycak bir yazı. :Bende kendi çalıştığım medya ajansına bir normal site birde mobil site tasarladım yanlız mobilden girenler otomatik olarak mobil siteme nasıl yönlendirme yapacam yada bu kodu nereye koyacam .htaccsess doyasınamı? bu konuda bilgilendirirsen çok sevinirim şimdiden teşekkürler.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>