4

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

Liath
  • 9,913
  • 9
  • 51
  • 81
Ramkumar R
  • 107
  • 1
  • 9

3 Answers3

6

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:

http://51degrees.mobi/

A good comparission can be found here:

http://dotnetslackers.com/articles/aspnet/Mobile-Device-Detection-and-Redirection-Using-ASP-NET.aspx

Craig Moore
  • 1,093
  • 1
  • 6
  • 15
3

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

Pedram
  • 15,766
  • 10
  • 44
  • 73
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
0

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.

TheJoe
  • 57
  • 10