I'm using my Page_Load code to detect mobile devices and re-direct users to a mobile home page. I want ALL Apple devices to also be redirected, so I'm including userAgents that include "Macintosh, Mac OS, Macbook and iMac as well--trying to "fake" the code
into thinking they are mobile devices. This code works great with iPhone or iPad, but a Macbook does not seem to get redirected. here is the Page_Load script if anyone has a suggestion on how I might get this to work (or a better method of catching Macs with
the same Page_Load):
Thanks. I like your example in the link, using the separate xml file for devices. Can I use the bool right in my page code file? I'm trying to do that, and I'm getting all sorts of syntax errors. Here it is:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.Page;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
public partial class Index : System.Web.UI.Page
{
bool bIsMobileDevice = false;
XDocument mobileDeviceDoc = XDocument.Load(Server.MapPath("AppData/MobileDevices.xml"));
var mobileDevices = from devices in mobileDeviceDoc.Root.Elements()
select devices;
foreach (XElement device in mobileDevices)
{
if (!string.IsNullOrEmpty(device.Value))
{
if ((Request.UserAgent.IndexOf(device.Value, StringComparison.OrdinalIgnoreCase)) > 0)
{
bIsMobileDevice = true;
break;
}
}
}//end:foreach loop
if (bIsMobileDevice)
{
Response.Redirect("Default.m.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
bIsMobileDevice();
}
}
Sorry that I'm trying to keep up and I'm still learning c# after switching from VB.net. I may be missing a reference to use your bool code snippet.
Thx,
RC
"Look at it go, Homer; this one's gonna go for miles!"
I'd like to use the bool to check user agents from an updatable .xml, but the code I have posted below is not working. It errors out on several syntax errors. Can you tell me if I'm missing a reference? Also, in my original code at the bottom of this post,
I added "AppleWebKit" as well as "Mac_PowerPC" and also "Mac OS X" but devices such as a Macbook Pro are not redirected. Is there anything you see in my original Page_Load that is not workable?
The issue is that iPhones and iPads are redirected, but Macbooks and iMacs are not. I would like those to be redirected, along with mobile devices, to the "Default.m.aspx" destination.
Thanks again for any suggestion.
RC
"Look at it go, Homer; this one's gonna go for miles!"
Again, thanks much for your time. I have two questions.
1) I added the bool to the Page_Load, but I still got syntax errors in VS until I added two assembly references:
using System.Linq;
using System.Xml.Linq;
Then, when I loaded the page, I got a server error: "The type or name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)."
2) If you look down at the start of this thread, the code I have for .IsMobileDevice will successfully redirect to my mobile default(default.m.aspx) for iPhones and iPads, but Macs (Macbook Pro, Macboook Air and iMac desktops) are not redirected. Is there
some security in mac OS that prevents these redirects from executing?
Thx much !
RC
"Look at it go, Homer; this one's gonna go for miles!"
Then, when I loaded the page, I got a server error: "The type or name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)."
2) If you look down at the start of this thread, the code I have for .IsMobileDevice will successfully redirect to my mobile default(default.m.aspx) for iPhones and iPads, but Macs (Macbook Pro, Macboook Air and iMac desktops) are not redirected. Is there
some security in mac OS that prevents these redirects from executing?
Because, Safari UserAgent is this -
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
If you try to check "AppleWebKit" in your code, then it should work for Apple MacBooks as well!
Sorry, I may be a little ignorant on some things...I tried to add a reference to 'System.Linq' but could not find it in .Net references tab. I saw System.Data.Linq AND System.XML.Linq, but I could not find 'System.Linq' in the reference listing unbder the
.Net tab. Do I need to bring it in from somewhere?
Also, In one of my test pages, I'm checking userAgent for "AppleWebKit", "Mac OS X" and "Macintosh" in the userAgent string. But I still do not get a redirect on a macbook.
"Look at it go, Homer; this one's gonna go for miles!"
using System;
using System.Collections.Generic;
using System.Linq;
ReidMelSam
lso, In one of my test pages, I'm checking userAgent for "AppleWebKit", "Mac OS X" and "Macintosh" in the userAgent string. But I still do not get a redirect on a macbook.
Open this website http://whatsmyuseragent.com/ in your MAC and see the User Agent string and get the unique string from that!
Yes, I have all three of those references on my page. I even put them in that order. I checked on my hosting account and the .net version is 4.0. But I cannot find 'System.Linq' to add it as a project reference.
Also, I have checked the userAgent using whatsmyuserAgent.com and then put the info into my userAgent string checker and I still do not get redirects from a macbook. By using 'compitible' I can even get my own windows PC to redirect, in effect making the
index.aspx page think it is a targeted mobile device. But I cannot get a macbook to redirect.
"Look at it go, Homer; this one's gonna go for miles!"
ReidMelSam
Participant
857 Points
270 Posts
Catch Macs in Mobile Device detect ?
Jan 28, 2013 10:42 PM|LINK
Hi,
I'm using my Page_Load code to detect mobile devices and re-direct users to a mobile home page. I want ALL Apple devices to also be redirected, so I'm including userAgents that include "Macintosh, Mac OS, Macbook and iMac as well--trying to "fake" the code into thinking they are mobile devices. This code works great with iPhone or iPad, but a Macbook does not seem to get redirected. here is the Page_Load script if anyone has a suggestion on how I might get this to work (or a better method of catching Macs with the same Page_Load):
protected void Page_Load(object sender, EventArgs e) { string strUserAgent = Request.UserAgent.ToString().ToLower(); if (strUserAgent != null) { if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") || strUserAgent.Contains("ipad") || strUserAgent.Contains("Macintosh") || strUserAgent.Contains("Macbook") || strUserAgent.Contains("Mac OS") || strUserAgent.Contains("SAMSUNG") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")) { Response.Redirect("Default.m.aspx"); } } }Thx!
RC
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: Catch Macs in Mobile Device detect ?
Jan 29, 2013 03:21 PM|LINK
Hi,
First of all Mac contains Safari browsers!
So try checking for this string - "AppleWebKit" in the UserAgent!
Moreover, i blogged the same detecting the device and redirecting here - http://roopeshreddy.wordpress.com/2012/06/24/detect-requesting-device-type-and-redirect-in-asp-net/
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
ReidMelSam
Participant
857 Points
270 Posts
Re: Catch Macs in Mobile Device detect ?
Jan 30, 2013 06:54 PM|LINK
Thanks. I like your example in the link, using the separate xml file for devices. Can I use the bool right in my page code file? I'm trying to do that, and I'm getting all sorts of syntax errors. Here it is:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.Page; using System.Web.UI.WebControls; using System.Xml; using System.Xml.Linq; public partial class Index : System.Web.UI.Page { bool bIsMobileDevice = false; XDocument mobileDeviceDoc = XDocument.Load(Server.MapPath("AppData/MobileDevices.xml")); var mobileDevices = from devices in mobileDeviceDoc.Root.Elements() select devices; foreach (XElement device in mobileDevices) { if (!string.IsNullOrEmpty(device.Value)) { if ((Request.UserAgent.IndexOf(device.Value, StringComparison.OrdinalIgnoreCase)) > 0) { bIsMobileDevice = true; break; } } }//end:foreach loop if (bIsMobileDevice) { Response.Redirect("Default.m.aspx"); } protected void Page_Load(object sender, EventArgs e) { bIsMobileDevice(); } }Sorry that I'm trying to keep up and I'm still learning c# after switching from VB.net. I may be missing a reference to use your bool code snippet.
Thx,
RC
ReidMelSam
Participant
857 Points
270 Posts
Re: Catch Macs in Mobile Device detect ?
Jan 31, 2013 07:41 PM|LINK
Hi Roopesh,
I'd like to use the bool to check user agents from an updatable .xml, but the code I have posted below is not working. It errors out on several syntax errors. Can you tell me if I'm missing a reference? Also, in my original code at the bottom of this post, I added "AppleWebKit" as well as "Mac_PowerPC" and also "Mac OS X" but devices such as a Macbook Pro are not redirected. Is there anything you see in my original Page_Load that is not workable?
The issue is that iPhones and iPads are redirected, but Macbooks and iMacs are not. I would like those to be redirected, along with mobile devices, to the "Default.m.aspx" destination.
Thanks again for any suggestion.
RC
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: Catch Macs in Mobile Device detect ?
Feb 01, 2013 03:30 PM|LINK
Hi,
Put the code in the Page_Load event!
public partial class Index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { bool bIsMobileDevice = false; XDocument mobileDeviceDoc = XDocument.Load(Server.MapPath("AppData/MobileDevices.xml")); var mobileDevices = from devices in mobileDeviceDoc.Root.Elements() select devices; foreach (XElement device in mobileDevices) { if (!string.IsNullOrEmpty(device.Value)) { if ((Request.UserAgent.IndexOf(device.Value, StringComparison.OrdinalIgnoreCase)) > 0) { bIsMobileDevice = true; break; } } }//end:foreach loop if (bIsMobileDevice) { Response.Redirect("Default.m.aspx"); } } }Hope it helps u...
Roopesh Reddy C
Roopesh's Space
ReidMelSam
Participant
857 Points
270 Posts
Re: Catch Macs in Mobile Device detect ?
Feb 04, 2013 02:40 PM|LINK
Hi Roopesh,
Again, thanks much for your time. I have two questions.
1) I added the bool to the Page_Load, but I still got syntax errors in VS until I added two assembly references:
Then, when I loaded the page, I got a server error: "The type or name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)."
2) If you look down at the start of this thread, the code I have for .IsMobileDevice will successfully redirect to my mobile default(default.m.aspx) for iPhones and iPads, but Macs (Macbook Pro, Macboook Air and iMac desktops) are not redirected. Is there some security in mac OS that prevents these redirects from executing?
Thx much !
RC
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: Catch Macs in Mobile Device detect ?
Feb 04, 2013 03:04 PM|LINK
Hi,
You need to add System.Linq assembly to your web application - http://msdn.microsoft.com/en-in/library/wkze6zky(v=vs.80).aspx
Since the code uses Linq!
Because, Safari UserAgent is this -
If you try to check "AppleWebKit" in your code, then it should work for Apple MacBooks as well!
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
ReidMelSam
Participant
857 Points
270 Posts
Re: Catch Macs in Mobile Device detect ?
Feb 04, 2013 04:13 PM|LINK
Sorry, I may be a little ignorant on some things...I tried to add a reference to 'System.Linq' but could not find it in .Net references tab. I saw System.Data.Linq AND System.XML.Linq, but I could not find 'System.Linq' in the reference listing unbder the .Net tab. Do I need to bring it in from somewhere?
Also, In one of my test pages, I'm checking userAgent for "AppleWebKit", "Mac OS X" and "Macintosh" in the userAgent string. But I still do not get a redirect on a macbook.
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: Catch Macs in Mobile Device detect ?
Feb 04, 2013 04:32 PM|LINK
Hi,
Did you tried adding like this?
Open this website http://whatsmyuseragent.com/ in your MAC and see the User Agent string and get the unique string from that!
Hope it helps u...
Roopesh Reddy C
Roopesh's Space
ReidMelSam
Participant
857 Points
270 Posts
Re: Catch Macs in Mobile Device detect ?
Feb 04, 2013 05:03 PM|LINK
Yes, I have all three of those references on my page. I even put them in that order. I checked on my hosting account and the .net version is 4.0. But I cannot find 'System.Linq' to add it as a project reference.
Also, I have checked the userAgent using whatsmyuserAgent.com and then put the info into my userAgent string checker and I still do not get redirects from a macbook. By using 'compitible' I can even get my own windows PC to redirect, in effect making the index.aspx page think it is a targeted mobile device. But I cannot get a macbook to redirect.