2

I have two templates in Umbraco. One for desktop and another for mobile. I have a small script which detects the user agent of the request and redirects the user accordingly.

If the request is made from desktop the user is redirected to desktop template with URL www.abc.com.

If a request is made from mobile the user is redirected to mobile template with url www.abc.com/?alttemplate=mobilehomepage

How to make the URL same for both desktop and mobile.

I am using Response.Redirect for redirection.

Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
aaviss
  • 401
  • 2
  • 10
  • 29
  • 1
    You _might_ be able to use [Server.Transfer](http://stackoverflow.com/questions/224569/server-transfer-vs-response-redirect) for this – DGibbs Apr 22 '13 at 10:19
  • @DGibbs Thanks a lot for you comment. When I use Server.Transfer("?alttemplate=mobiletemplate"); I am getting System.ArgumentNullException – aaviss Apr 22 '13 at 10:21
  • That looks like a query string value and not a page? Shouldn't the parameter be something like: `www.abc.com/?alttemplate=mobilehomepage`? – DGibbs Apr 22 '13 at 10:23
  • I tried Server.Transfer("www.abc.com/?alttemplate=mobilehomepage"); It gives System.Web.HttpException:Error executing child request for www.abc.com/ – aaviss Apr 22 '13 at 10:29
  • Try `"~/?alttemplate=mobilehomepage"`. Also, consider reading up on [Server.Transfer](http://msdn.microsoft.com/en-us/library/ms525800(v=vs.90).aspx) which may or may not help – DGibbs Apr 22 '13 at 10:30
  • A valid and frequently used url for your mobile website could also be m.abc.com instead of using the querystring params. Try to use the UrlRewriting option as Andrei answered below. – Martijn van der Put Apr 22 '13 at 10:51
  • @DGibbs "~/?alttemplate=mobilehomepage" is also giving System.Web.HttpException:Error executing child request for / – aaviss Apr 22 '13 at 11:31
  • @Martijn I don't have separate instance for mobile like m.abc.com. So I have to use alttemplate only. – aaviss Apr 22 '13 at 11:32

2 Answers2

5

All the umbraco template decisions run through the default.aspx(.cs), and programatically you can change the template by overriding the Page PreInit method.

So this is how I achieved this in the default.aspx.cs file with templatenameMobile, templatenameDesktop & templateNameTablet templates, Obviously you need methods for saying whether you are serving to a mobile, tablet or desktop (which you can deduce from the user agent):

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            string userAgent = Request.UserAgent;
            bool isTablet = IsTablet(userAgent);
            bool isMobile = IsMobile(userAgent);

            int templateId = umbraco.NodeFactory.Node.GetCurrent().template;
            umbraco.template template = new umbraco.template(templateId);
            string templateName = StripDevice(template.TemplateAlias);

            if (isTablet)
            {
                Page.MasterPageFile = GetTabletMaster(templateName);
            }
            else if (isMobile)
            {
                Page.MasterPageFile = GetMobileMaster(templateName);
            }
            else
            {
                Page.MasterPageFile = GetDesktopMaster(templateName);
            }

}

    public string GetMobileMaster(string templateName)
    {
        try
        {
            MasterPage masterPage = new MasterPage();
            masterPage.MasterPageFile = string.Format("/masterpages/{0}mobile.master", templateName);
            if (masterPage == null)
            {
                masterPage.MasterPageFile = string.Format("/masterpages/{0}desktop.master", templateName);
            }
            if (masterPage == null)
            {
                return Page.MasterPageFile;
            }
            else
            {
                return masterPage.MasterPageFile;
            }
        }
        catch (Exception ex)
        {
            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, umbraco.BusinessLogic.User.GetUser(0), -1, "Switch template to MOBILE fail " + templateName + " : " + ex.Message);
            return Page.MasterPageFile;
        }
    }
amelvin
  • 8,919
  • 4
  • 38
  • 59
  • Thanks a lot. where can I find default.aspx.cs file. Please help. In the root folder I have only default.aspx.cs file with a single line **<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="True" Inherits="umbraco.UmbracoDefault" trace="true" validateRequest="false" %>** Should I append your code their – aaviss Apr 24 '13 at 03:51
  • Its a bit more complicated than that; umbraco.UmbracoDefault code is in umbraco.dll and you will need to switch to inheriting from a partial class and need to know how the page event lifecycle works - if you're new to this area then it is going to be tricky. The page life cycle (http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx) should be a place to start to see how you can interupt the standard flow. – amelvin Apr 24 '13 at 08:08
2

You can try to use UrlRewriting. It's included into Umbraco. Try to play with config\UrlRewriting.config

Here is the documentation:

http://www.urlrewriting.net/160/en/documentation/documentation/documentation/documentation/documentation/documentation.html

Andrei
  • 42,814
  • 35
  • 154
  • 218