locked
Mobile Detection/Redirect Script Not Working RRS feed

  • Question

  • User-1673721697 posted

    Guys/Gals,

    I'll start off by saying this is NOT my field of expertise!  I did a lot of searching and found the mobile redirect script which I inserted into my web.config file:

    <script runat="server" language="c#">

    public void Page_Load(Object sender, EventArgs e)

    {

    if (Request.Browser["IsMobileDevice"] == "true" )

    {

    Response.Redirect("index.xhtml");

    }

    else

    {

    Response.Redirect("Default.aspx");

    }

    }

    </script>

     

    I wasn't 100% sure where to drop this into the config file as the online store is pretty much the only data in the file but I thought under <settings> would make the most since, however nothing is taking place, I attemped to view the site on both an iPhone and using Opera's virtual mobile browser and they both loaded our full wesbite.

    Also as a side note, I'm not sure if the technology has changed dramatically but its really kind of difficult to find information about making this stuff work (mobile redirects in .ASP) the intial document that was referenced several times over has also been pinned as not working on a lot of today's devices.

     Help is greatly appreciated-

    Thanks,

     

    Dean

     

    PS: the site in question http://futrellautowerks.com

    Thursday, November 6, 2008 8:00 PM

Answers

  • User-861818263 posted

    if (Request.Headers["X-Wap-Profile"] != null) { if (Request.Headers["X-Wap-Profile"].ToString().Length > 0) { IsMobi = true; Response.Redirect("index.aspx", true); } Else { Response.Redirect("web/index.aspx", true); } }

     

     

     

    http://www.codeproject.com/KB/mobile/All-about-a-mobile-websit.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2008 4:27 AM
  • User1634317999 posted

    Thanks for the tip, I'm still having issues though, by head in the .aspx do you mean the master document?

    No. I mean the .aspx file where you like to insert your redirect script.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 9, 2008 4:05 AM

All replies

  • User1634317999 posted

     I did a lot of searching and found the mobile redirect script which I inserted into my web.config file:

    <script runat="server" language="c#">

    public void Page_Load(Object sender, EventArgs e)

    {

    if (Request.Browser["IsMobileDevice"] == "true" )

    {

    Response.Redirect("index.xhtml");

    }

    else

    {

    Response.Redirect("Default.aspx");

    }

    }

    </script>

    Hello,

    you have to insert this script in the head of your .aspx pages and not in the web.config. 

     

     

    Friday, November 7, 2008 9:32 AM
  • User-1673721697 posted

    Thanks for the tip, I'm still having issues though, by head in the .aspx do you mean the master document? (example default.aspx) if so I tried dropping the code into both the top and bottom of that doc and either way caused the webpage to load blank.

     The default.aspx code is as follows:

    <%@ Page Language="C#" MasterPageFile="~/Layouts/Scriptlet.master" Inherits="CommerceBuilder.Web.UI.AbleCommercePage" Title="Welcome To Futrell Autowerks" %>
    <%@ Register Assembly="CommerceBuilder.Web" Namespace="CommerceBuilder.Web.UI.WebControls.WebParts" TagPrefix="cb" %>
    <asp:Content runat="server" ContentPlaceHolderID="PageContent">
        <cb:ScriptletPart ID="HomePage" runat="server" Layout="Three Column" Content="Home Page" Sidebar="Standard Sidebar 1" Sidebar2="Standard Sidebar 2" Header="Standard Header" Footer="Standard Footer" Title="Home Page" AllowClose="False" AllowMinimize="false" />
    </asp:Content>

     I looked at putting the code in the html document called header but that didn't look right as it was only basic html language and conlib call-outs.

    Friday, November 7, 2008 2:22 PM
  • User-861818263 posted

    if (Request.Headers["X-Wap-Profile"] != null) { if (Request.Headers["X-Wap-Profile"].ToString().Length > 0) { IsMobi = true; Response.Redirect("index.aspx", true); } Else { Response.Redirect("web/index.aspx", true); } }

     

     

     

    http://www.codeproject.com/KB/mobile/All-about-a-mobile-websit.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2008 4:27 AM
  • User1634317999 posted

    Thanks for the tip, I'm still having issues though, by head in the .aspx do you mean the master document?

    No. I mean the .aspx file where you like to insert your redirect script.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 9, 2008 4:05 AM
  • User981004358 posted

    You can also use 51degrees.mobi Foundation API which detects all mobile devices. It is a .NET open source available here http://51degrees.codeplex.com/releases.

    It easily detects if the request is coming from any mobile device and redirects user to mobile landing page. It has some user friendly samples to try out.

    Thanks

    Amaresh

     

    Monday, August 9, 2010 1:11 AM
  • User-1034524349 posted

    Does this mean, I can’t put that code in the head of my default masterpage. I can’t put it in the head of my Default.axpx page because Default.axpx is attached to a master page that has a header section???

    Tuesday, August 30, 2011 4:42 AM
  • User1918766559 posted

    Hi

    Mobile redirection does not need the script to be kept in web.config.

    You can add simply a new file like 'MobileRedirectHandler.cs" & add the following code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace MobileHandler
    {
        public class MobileRedirectHandler : IHttpModule
        {
            public string RedirectToPath = "/mobile";

            public void Init(HttpApplication app)
            {
                app.BeginRequest += new EventHandler(this.OnBeginRequest);
            }
            public void Dispose() { }
            public void OnBeginRequest(object o, EventArgs e)
            {
                HttpContext context = HttpContext.Current;

                if (context.Request.Browser.IsMobileDevice &&
                    context.Request.RawUrl.LastIndexOf(RedirectToPath) < 0)
                    context.Response.Redirect(RedirectToPath, true);
            }
        }
    }

    & make the changes in Web.Config for IIS 7.0

    <modules>
          <add name="MobileHandler" type="MobileHandler.MobileRedirectHandler, MobileHandler" preCondition="managedHandler" />
    </modules>

     

    Thanks,

    Anindita

    http://anindita9.wordpress.com

    Wednesday, September 14, 2011 12:30 AM
  • User-1034524349 posted

    Thanks Anindita,

    I was able to do it like below, Inside my Home.Master.cs.

    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("blackberry") || strUserAgent.Contains("mobile") ||
                    strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") ||
                    strUserAgent.Contains("palm"))
                {
                    Response.Redirect("mobile/Default.aspx");
                }
            }
       }

    Its not perfect as I am still trying to work out how to let the user view the desktop pagefrom the mobile site if they want to, but for now but it works,

    As far as I’m concerned, In most cases I don’t want them to???

    Cheers,

    Mike.

    Wednesday, September 14, 2011 2:30 AM