Wednesday, December 30, 2015

Detect requesting device type and redirect in ASP.NET and JQuery, Java script

In ASP.NET WebForms we have an option to check whether the requested device is Mobile or not – Request.IsMobileDevice, which is not appropriate in most of the scenarios! I mean, it’s failing to detect the device type correctly!
Now, we discuss about custom solution for ASP.NET WebForms – detect the device type and redirecting!
Suppose if you have two versions of your web application – Mobile & Desktop, and you may wish to get the device information  and redirect the user to the appropriate web app based on the requested device

    private string DeviceName()
        string DeviceName = "Other"; // Desktop
        string[] MobileDevices = new string[] { "iPhone", "iPad","iPod","BlackBerry",
                                                     "Nokia", "Android", "WindowsPhone",
        foreach (string MobileDeviceName in MobileDevices)
            if ((Request.UserAgent.IndexOf(MobileDeviceName, StringComparison.OrdinalIgnoreCase)) > 0)
                DeviceName = MobileDeviceName;

        }//end:foreach loop

        return DeviceName;

if (DeviceName() == "Android")
            lblMesage.Text = "Mobile Device    " + DeviceName();
            lblMesage.Text = "other device";


in Javascript 

 $(document).ready(function () {           
            var ua = navigator.userAgent.toLowerCase();
            var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
            if (isAndroid) {
                 window.location= "";


No comments:

Post a Comment