简体   繁体   English

页面重定向而不更改URL-Umbraco | C#

[英]Page redirect without changing the URL-Umbraco | C#

I have two templates in Umbraco. 我在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 . 如果请求是从桌面发出的,则用户将被重定向到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 如果通过移动设备发出请求,则会将用户重定向到移动模板,网址为www.abc.com/?alttemplate=mobilehomepage

How to make the URL same for both desktop and mobile. 如何使桌面和移动设备的URL相同。

I am using Response.Redirect for redirection. 我正在使用Response.Redirect进行重定向。

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. 所有umbraco模板决策都通过default.aspx(.cs)运行,并且您可以通过编程方式通过重写Page PreInit方法来更改模板。

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): 所以这就是我在default.aspx.cs文件中使用templatenameMobile,templatenameDesktop和templateNameTablet模板实现这一点的方法。显然,你需要一些方法来说明你是服务于移动设备,平板电脑还是桌面(你可以从用户代理中推断出来) :

        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. 您可以尝试使用UrlRewriting。 It's included into Umbraco. 它被包含在Umbraco中。 Try to play with config\\UrlRewriting.config 尝试玩config \\ UrlRewriting.config

Here is the documentation: 这里是文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM