首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >页面重定向而不更改Umbraco\ C#

页面重定向而不更改Umbraco\ C#
EN

Stack Overflow用户
提问于 2013-04-22 02:17:26
回答 2查看 2.1K关注 0票数 2

我在Umbraco中有两个模板。一个用于桌面,另一个用于移动。我有一个小脚本,它检测请求的用户代理并相应地重定向用户。

如果请求是从桌面发出的,则用户将被重定向到带有URL www.abc.com的桌面模板。

如果从移动设备发出请求,则用户将被重定向到使用url www.abc.com/?alttemplate=mobilehomepage的移动模板。

如何使桌面和移动的网址相同.

我使用Response.Redirect重定向。

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-22 05:37:48

所有的umbraco模板决策都通过default.aspx(.cs)运行,并且可以编程地通过重写PreInit方法来更改模板。

因此,我是如何在default.aspx.cs文件中使用templatenameMobile、templatenameDesktop和templateNameTablet模板实现这一目标的,显然您需要一些方法来说明您是在为移动、平板或桌面服务(您可以从用户代理中推断这些方法):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        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;
        }
    }
票数 5
EN

Stack Overflow用户

发布于 2013-04-22 02:45:14

您可以尝试使用UrlRewriting。它包括在Umbraco中。试着玩config\UrlRewriting.config

以下是文件:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16144629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文