OK now this works perfectly and the user get redirected but say they want to go to the desktop site then what? Because this code will just to a redirect again.. how can I prevent this?
I can't figure it out. My main site uses RazorC (CMS) the mobil was just hand coded using asp.net and master/and nested pages. At the bottom of mobile site is "return to desktop" site.
The thing is out is it going to pass on the information? That peice of code above is in the .cshtml layout page and it redirects the person to mobile site if it detects it.
Thanks for that Mike but there is another problem now, I just checked on my Android device and when I click "Desktop SIte" it just redirects back to the mobile site, it's not supposed to since the tracking cookie is saved and MobileDevice = false. What am
I doing wrong?
You need to check the value of the cookie before redirecting if the device is a mobile device, since the cookie value overrides the mobile device check:
I tried this on Android and my dekstop PC (got a plugin to cahnge user agent) and it redirects to mobile site, but then when user goes to desktop site it still redirects back, I checked cookies and it did have the cookie Mobile Device with the above value.
So basically if there is a tracking cookie named Mobile Device and value IgnoreMobileDevice then MobileDevice should be false. However for some reason it still evaulates to true and redirects.
Now with the second part it says if (MobileDevice == true). The thing is if the tracking cookie is in the browser, then MobileDevice is not equal to true it's false so it should not redirect, but it always does.
You have incorrect code that is why. Even if the MobileDevice is set to false you still check if strUserAgent contains "android" etc. so yes users will be redirected to mobile site because your code will detect "android"
Take a good look at your IF statement, what you do is:
if mobile device OR if contains "android" OR if contains ....
Anyway (i did not tetst it) but i think this is work for you:
CriticalErro...
Member
423 Points
394 Posts
mobile redirect, and return to desktop site
Jul 06, 2012 12:15 AM|LINK
Hey guys having a little problem I got this code:
string strUserAgent = Request.UserAgent.ToString().ToLower(); if (strUserAgent != null){ if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("android") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")) { Response.Redirect("http://m.thecodingguys.net"); } }OK now this works perfectly and the user get redirected but say they want to go to the desktop site then what? Because this code will just to a redirect again.. how can I prevent this?
My Site | My Blog
Ken Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: mobile redirect, and return to desktop site
Jul 06, 2012 01:45 AM|LINK
I guess you could use a Query string to do this. Change the if statement so if there is a query string like IgnoreMobile=="true" it will not redirect
Space Coast .Net User Group
CriticalErro...
Member
423 Points
394 Posts
Re: mobile redirect, and return to desktop site
Jul 07, 2012 09:49 PM|LINK
I can't figure it out. My main site uses RazorC (CMS) the mobil was just hand coded using asp.net and master/and nested pages. At the bottom of mobile site is "return to desktop" site.
The thing is out is it going to pass on the information? That peice of code above is in the .cshtml layout page and it redirects the person to mobile site if it detects it.
My Site | My Blog
CriticalErro...
Member
423 Points
394 Posts
Re: mobile redirect, and return to desktop site
Jul 07, 2012 11:09 PM|LINK
OK I have done this so far maybe someone get help. When the user clicks on Desktop Site a tracking cookie is added in their browser. Here is the code:
Response.Cookies["MobileDevice"].Value = "IgnoreMobileDevice"; Response.Cookies["MobileDevice"].Expires = DateTime.Now.AddDays(30);Now here is the code for the main site domain thecodngguys..
@{ string strUserAgent = Request.UserAgent.ToString().ToLower(); bool MobileDevice = Request.Browser.IsMobileDevice; if (strUserAgent != null){ if (MobileDevice == true || strUserAgent.Contains("iphone") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("android") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")) { Response.Redirect("http://localhost:52097",false); } else { if (Request.Cookies["MobileDevice"].Value == "IgnoreMobileDevice"){ MobileDevice = false; } } } }Now I got an error because if the cookie is not in their browser then you get
Object reference not set to an instance of an object.
And it points to line:
if (Request.Cookies["MobileDevice"].Value == "IgnoreMobileDevice")Can someone clear this up?My Site | My Blog
Mikesdotnett...
All-Star
154951 Points
19870 Posts
Moderator
MVP
Re: mobile redirect, and return to desktop site
Jul 08, 2012 06:49 AM|LINK
You should always check to see if a cookie is null or not before attempting to read values from it:
} else { if(Request.Cookies["MobileDevice"] != null){ if (Request.Cookies["MobileDevice"].Value == "IgnoreMobileDevice"){ MobileDevice = false; } } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
CriticalErro...
Member
423 Points
394 Posts
Re: mobile redirect, and return to desktop site
Jul 08, 2012 11:41 AM|LINK
Thanks for that Mike but there is another problem now, I just checked on my Android device and when I click "Desktop SIte" it just redirects back to the mobile site, it's not supposed to since the tracking cookie is saved and MobileDevice = false. What am I doing wrong?
My Site | My Blog
Mikesdotnett...
All-Star
154951 Points
19870 Posts
Moderator
MVP
Re: mobile redirect, and return to desktop site
Jul 08, 2012 04:40 PM|LINK
You need to check the value of the cookie before redirecting if the device is a mobile device, since the cookie value overrides the mobile device check:
@{ string strUserAgent = Request.UserAgent.ToString().ToLower(); bool MobileDevice = Request.Browser.IsMobileDevice; if(Request.Cookies["MobileDevice"] != null){ if (Request.Cookies["MobileDevice"].Value == "IgnoreMobileDevice"){ MobileDevice = false; } } if (strUserAgent != null){ if (MobileDevice == true || strUserAgent.Contains("iphone") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("android") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")){ Response.Redirect("http://localhost:52097",false); } } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
CriticalErro...
Member
423 Points
394 Posts
Re: mobile redirect, and return to desktop site
Jul 08, 2012 06:09 PM|LINK
I tried this on Android and my dekstop PC (got a plugin to cahnge user agent) and it redirects to mobile site, but then when user goes to desktop site it still redirects back, I checked cookies and it did have the cookie Mobile Device with the above value.
The first part is this:
bool MobileDevice = Request.Browser.IsMobileDevice; if(Request.Cookies["MobileDevice"] != null){ if (Request.Cookies["MobileDevice"].Value == "IgnoreMobileDevice"){ MobileDevice = false; } }So basically if there is a tracking cookie named Mobile Device and value IgnoreMobileDevice then MobileDevice should be false. However for some reason it still evaulates to true and redirects.
if (strUserAgent != null){ if (MobileDevice == true || strUserAgent.Contains("iphone") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("android") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")){ Response.Redirect("http://m.thecodingguys.net",false); } } }Now with the second part it says if (MobileDevice == true). The thing is if the tracking cookie is in the browser, then MobileDevice is not equal to true it's false so it should not redirect, but it always does.
My Site | My Blog
TOMCIO
Contributor
3636 Points
832 Posts
Re: mobile redirect, and return to desktop site
Jul 08, 2012 07:50 PM|LINK
You have incorrect code that is why. Even if the MobileDevice is set to false you still check if strUserAgent contains "android" etc. so yes users will be redirected to mobile site because your code will detect "android"
Take a good look at your IF statement, what you do is:
if mobile device OR if contains "android" OR if contains ....
Anyway (i did not tetst it) but i think this is work for you:
string strUserAgent = Request.UserAgent.ToString().ToLower(); bool MobileDevice = Request.Browser.IsMobileDevice; if (strUserAgent != null){ if (strUserAgent.Contains("iphone") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("android") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")){ MobileDevice=true; } } if(Request.Cookies["MobileDevice"] != null){ if (Request.Cookies["MobileDevice"].Value == "IgnoreMobileDevice"){ MobileDevice = false; } } if (MobileDevice == true ){ Response.Redirect("http://m.thecodingguys.net",false); }Web: Chicago Website Design
r@zorC - Open Source ASP.net CMS for WebMatrix
CriticalErro...
Member
423 Points
394 Posts
Re: mobile redirect, and return to desktop site
Jul 08, 2012 10:52 PM|LINK
Still makes the redirect is there anyway to tell the if statement to stop and no longer run?
Basically if the cookie has been detected no longer run the bottom if statement..
My Site | My Blog