Detect mobile browser and redirect

11,207

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.

Share:
11,207

Related videos on Youtube

user1628753
Author by

user1628753

Updated on June 22, 2022

Comments

  • user1628753
    user1628753 almost 2 years

    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.)

  • user1628753
    user1628753 over 11 years
    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
    user1628753 over 11 years
    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?
  • istepaniuk
    istepaniuk over 11 years
    Is not a NEED, but 51Degrees [they claim] solves some problems such as new mobile browsers not being detected, etc.
  • John Washam
    John Washam over 10 years
    @user1628753, Request.Browser.IsMobileDevice is built-in to C#, whereas 51Degrees is a wholly separate Nuget package. They are independent of each other.

Related