Reputation: 107
I am using aspx pages in my website. when user open my Desktop Website in mobile I want to go be redirected to my Mobile Website. I am using C#.
Upvotes: 4
Views: 6738
Reputation: 75
What about using a simple page, with no MasterPage, that uses Request.Browser.IsMobileDevice (or other method) to determine mobile or not and then redirect to the appropriate LogOn page for mobile or desktop?
I am also uses a querystring in the URL:
ForceMobile = Request.QueryString["ForceMobile"];
//
if (Request.Browser.IsMobileDevice || ForceMobile == "Yes")
{
Response.Redirect("~/_Mobile/mLogOn.aspx", false);
}
else
{
Response.Redirect("~/LogOn.aspx", false);
}
The benefits of this are ease of debugging and no post-back events caused by redirects directly in LogOn pages.
Upvotes: 0
Reputation: 8161
You can get UserAgent
in C# using Request.UserAgent
.
Try this:
string strUA = Request.UserAgent.Trim().ToLower();
bool isMobile = false;
if (strUA .Contains("ipod") || strUA .Contains("iphone"))
isMobile = true;
if (strUA .Contains("android"))
isMobile = true;
if (strUA .Contains("opera mobi"))
isMobile = true;
if (strUA .Contains("windows phone os") && strUA .Contains("iemobile"))
isMobile = true;
if (strUA .Contains("palm")
isMobile = true;
bool MobileDevice = Request.Browser.IsMobileDevice;
if(isMobile == true && MobileDevice == true)
{
string Url = ""; // Put your mobile site url
Response.Redirect(Url);
}
Note: IsMobileDevice
is not actively updated with new browsers.
Some of the popular mobile devices/browsers won’t be detected using this way because ASP.NET browser files are not supported in Opera Mobile or Android devices.
As a fix of this problem is : use 51Degrees.Mobi package. 51Degrees.Mobi package
Read this article: Mobile Device Detection
Upvotes: 3
Reputation: 1089
You can either check for Request.Browser["IsMobileDevice"] == "true"
using the framework as explained here:
http://msdn.microsoft.com/en-us/library/fhhycabe%28v=vs.90%29.aspx
Or you can use 51Degrees.mobi which is shown here:
A good comparission can be found here:
http://dotnetslackers.com/articles/aspnet/Mobile-Device-Detection-and-Redirection-Using-ASP-NET.aspx
Upvotes: 6