Search code examples
c#asp.netumbracoumbraco5umbraco-blog

Page redirect without changing the URL-Umbraco | C#


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.


Solution

  • 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;
            }
        }