4

I want to use my .cs codebehind, either Page_PreInit or Page_Load to detect mobile browser and to redirect. I ran across this:

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    if (Request.Browser.IsMobileDevice) 
    { 
        { 
          Response.Redirect("~/default_mobile.aspx"); 
        }

    } 
} 

It doesn't appear to work. Can someone suggest a correction? Also, do you know of an example of NOT redirecting, but merely replacing an element on the .aspx page with another (i.e; replacing a Silverlight movie with a still image for an iOS device.)

istepaniuk
  • 4,016
  • 2
  • 32
  • 60
user1628753
  • 41
  • 1
  • 1
  • 2

2 Answers2

2

This MSDN document explains how to use .IsMobileDevice in the context of a Page_Load. It should be trivial to adapt it to your needs.

Check also this other answer

And the 51Degrees, a class library that detects mobile devices and browsers, enhancing the information available to .NET.

Community
  • 1
  • 1
istepaniuk
  • 4,016
  • 2
  • 32
  • 60
  • Hi, thanks for this...I read it and tried the test on a page. It loads, it reads the code, but it displays "Browser is NOT a mobile device" for everything, including iPad and iPhone. Do I need to add assembly references to the top of the C# codebehind page? – user1628753 Jan 15 '13 at 14:42
  • Thanks again...I'm not clear on one thing--for my above code to work ((Request.Browser.IsMobileDevice) -- do I NEED to have a class library like 51Degrees installed? – user1628753 Jan 15 '13 at 19:23
  • Is not a NEED, but 51Degrees [they claim] solves some problems such as new mobile browsers not being detected, etc. – istepaniuk Jan 15 '13 at 19:25
  • @user1628753, `Request.Browser.IsMobileDevice` is built-in to C#, whereas 51Degrees is a wholly separate Nuget package. They are independent of each other. – John Washam Aug 21 '13 at 18:43
0

Add A global application class in the project and write the code in Session_Start

protected void Session_Start(object sender, EventArgs e)
{

            HttpRequest httpRequest = HttpContext.Current.Request;
            if (httpRequest.Browser.IsMobileDevice)
            {
                string path = httpRequest.Url.PathAndQuery;
               bool isOnMobilePage = path.StartsWith("/Mobile/",
                                                      StringComparison.OrdinalIgnoreCase);
                if (!isOnMobilePage)
                {
                    string redirectTo = "~/Mobile/";

                    // Could also add special logic to redirect from certain 
                    // recognised pages to the mobile equivalents of those 
                    // pages (where they exist). For example,
                    // if (HttpContext.Current.Handler is UserRegistration)
                    //     redirectTo = "~/Mobile/RegistrationMobile.aspx";

                    HttpContext.Current.Response.Redirect(redirectTo);
                }
            }   
        }
mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
Chenthil
  • 296
  • 4
  • 9