Вопрос

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.

Это было полезно?

Решение

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

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top